^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * This file is subject to the terms and conditions of the GNU General Public
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * License. See the file "COPYING" in the main directory of this archive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2004, 2005 MIPS Technologies, Inc. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 2013 Imagination Technologies Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <asm/vpe.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) static int major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) void cleanup_tc(struct tc *tc)
^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) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static ssize_t store_kill(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct vpe *vpe = get_vpe(aprp_cpu_index());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct vpe_notifications *notifier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) list_for_each_entry(notifier, &vpe->notify, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) notifier->stop(aprp_cpu_index());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) release_progmem(vpe->load_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) vpe->state = VPE_STATE_UNUSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static DEVICE_ATTR(kill, S_IWUSR, NULL, store_kill);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static ssize_t ntcs_show(struct device *cd, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct vpe *vpe = get_vpe(aprp_cpu_index());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return sprintf(buf, "%d\n", vpe->ntcs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static ssize_t ntcs_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct vpe *vpe = get_vpe(aprp_cpu_index());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) unsigned long new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) ret = kstrtoul(buf, 0, &new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /* APRP can only reserve one TC in a VPE and no more. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (new != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) vpe->ntcs = new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static DEVICE_ATTR_RW(ntcs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static struct attribute *vpe_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) &dev_attr_kill.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) &dev_attr_ntcs.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) ATTRIBUTE_GROUPS(vpe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) static void vpe_device_release(struct device *cd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) kfree(cd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static struct class vpe_class = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) .name = "vpe",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) .dev_release = vpe_device_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) .dev_groups = vpe_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static struct device vpe_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) int __init vpe_module_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct vpe *v = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct tc *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (!cpu_has_mipsmt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) pr_warn("VPE loader: not a MIPS MT capable processor\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (num_possible_cpus() - aprp_cpu_index() < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) pr_warn("No VPEs reserved for AP/SP, not initialize VPE loader\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) "Pass maxcpus=<n> argument as kernel argument\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) major = register_chrdev(0, VPE_MODULE_NAME, &vpe_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (major < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) pr_warn("VPE loader: unable to register character device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) err = class_register(&vpe_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) pr_err("vpe_class registration failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) goto out_chrdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) device_initialize(&vpe_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) vpe_device.class = &vpe_class,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) vpe_device.parent = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) dev_set_name(&vpe_device, "vpe_sp");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) vpe_device.devt = MKDEV(major, VPE_MODULE_MINOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) err = device_add(&vpe_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) pr_err("Adding vpe_device failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) goto out_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) t = alloc_tc(aprp_cpu_index());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (!t) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) pr_warn("VPE: unable to allocate TC\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) goto out_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) /* VPE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) v = alloc_vpe(aprp_cpu_index());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (v == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) pr_warn("VPE: unable to allocate VPE\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) kfree(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) goto out_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) v->ntcs = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) /* add the tc to the list of this vpe's tc's. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) list_add(&t->tc, &v->tc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /* TC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) t->pvpe = v; /* set the parent vpe */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) out_dev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) device_del(&vpe_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) out_class:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) class_unregister(&vpe_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) out_chrdev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) unregister_chrdev(major, VPE_MODULE_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) void __exit vpe_module_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) struct vpe *v, *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) device_del(&vpe_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) class_unregister(&vpe_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) unregister_chrdev(major, VPE_MODULE_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) /* No locking needed here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) list_for_each_entry_safe(v, n, &vpecontrol.vpe_list, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (v->state != VPE_STATE_UNUSED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) release_vpe(v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }