^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) 2005 MIPS Technologies, Inc. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 2005, 06 Ralf Baechle (ralf@linux-mips.org)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright (C) 2013 Imagination Technologies Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kernel.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/syscalls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/moduleloader.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/atomic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <asm/mipsmtregs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <asm/mips_mt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <asm/processor.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <asm/rtlx.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <asm/setup.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <asm/vpe.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static int sp_stopping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct rtlx_info *rtlx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct chan_waitqueues channel_wqs[RTLX_CHANNELS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct vpe_notifications rtlx_notify;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) void (*aprp_hook)(void) = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) EXPORT_SYMBOL(aprp_hook);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static void __used dump_rtlx(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) pr_info("id 0x%lx state %d\n", rtlx->id, rtlx->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) for (i = 0; i < RTLX_CHANNELS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct rtlx_channel *chan = &rtlx->channel[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) pr_info(" rt_state %d lx_state %d buffer_size %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) chan->rt_state, chan->lx_state, chan->buffer_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) pr_info(" rt_read %d rt_write %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) chan->rt_read, chan->rt_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) pr_info(" lx_read %d lx_write %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) chan->lx_read, chan->lx_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) pr_info(" rt_buffer <%s>\n", chan->rt_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) pr_info(" lx_buffer <%s>\n", chan->lx_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) /* call when we have the address of the shared structure from the SP side. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) static int rtlx_init(struct rtlx_info *rtlxi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (rtlxi->id != RTLX_ID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) pr_err("no valid RTLX id at 0x%p 0x%lx\n", rtlxi, rtlxi->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return -ENOEXEC;
^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) rtlx = rtlxi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) /* notifications */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) void rtlx_starting(int vpe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) sp_stopping = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) /* force a reload of rtlx */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) rtlx = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) /* wake up any sleeping rtlx_open's */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) for (i = 0; i < RTLX_CHANNELS; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) wake_up_interruptible(&channel_wqs[i].lx_queue);
^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) void rtlx_stopping(int vpe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) sp_stopping = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) for (i = 0; i < RTLX_CHANNELS; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) wake_up_interruptible(&channel_wqs[i].lx_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) int rtlx_open(int index, int can_sleep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct rtlx_info **p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct rtlx_channel *chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) enum rtlx_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (index >= RTLX_CHANNELS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) pr_debug("rtlx_open index out of range\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (atomic_inc_return(&channel_wqs[index].in_open) > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) pr_debug("rtlx_open channel %d already opened\n", index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (rtlx == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) p = vpe_get_shared(aprp_cpu_index());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (p == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (can_sleep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) ret = __wait_event_interruptible(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) channel_wqs[index].lx_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) (p = vpe_get_shared(aprp_cpu_index())));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) pr_debug("No SP program loaded, and device opened with O_NONBLOCK\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) ret = -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) smp_rmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (*p == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (can_sleep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) DEFINE_WAIT(wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) prepare_to_wait(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) &channel_wqs[index].lx_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) &wait, TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) smp_rmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (*p != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (!signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) ret = -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) finish_wait(&channel_wqs[index].lx_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) pr_err(" *vpe_get_shared is NULL. Has an SP program been loaded?\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) ret = -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) goto out_fail;
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if ((unsigned int)*p < KSEG0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) pr_warn("vpe_get_shared returned an invalid pointer maybe an error code %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) (int)*p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) ret = -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) ret = rtlx_init(*p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) goto out_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) chan = &rtlx->channel[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) state = xchg(&chan->lx_state, RTLX_STATE_OPENED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (state == RTLX_STATE_OPENED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) out_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) smp_mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) atomic_dec(&channel_wqs[index].in_open);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) smp_mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) out_ret:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) int rtlx_release(int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (rtlx == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) pr_err("rtlx_release() with null rtlx\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) rtlx->channel[index].lx_state = RTLX_STATE_UNUSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) unsigned int rtlx_read_poll(int index, int can_sleep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) struct rtlx_channel *chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (rtlx == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) chan = &rtlx->channel[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) /* data available to read? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (chan->lx_read == chan->lx_write) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if (can_sleep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) int ret = __wait_event_interruptible(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) channel_wqs[index].lx_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) (chan->lx_read != chan->lx_write) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) sp_stopping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (sp_stopping)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) return (chan->lx_write + chan->buffer_size - chan->lx_read)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) % chan->buffer_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) static inline int write_spacefree(int read, int write, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) if (read == write) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * Never fill the buffer completely, so indexes are always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * equal if empty and only empty, or !equal if data available
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return size - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) return ((read + size - write) % size) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) unsigned int rtlx_write_poll(int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) struct rtlx_channel *chan = &rtlx->channel[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) return write_spacefree(chan->rt_read, chan->rt_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) chan->buffer_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) ssize_t rtlx_read(int index, void __user *buff, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) size_t lx_write, fl = 0L;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) struct rtlx_channel *lx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) unsigned long failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (rtlx == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) lx = &rtlx->channel[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) mutex_lock(&channel_wqs[index].mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) smp_rmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) lx_write = lx->lx_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) /* find out how much in total */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) count = min(count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) (size_t)(lx_write + lx->buffer_size - lx->lx_read)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) % lx->buffer_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) /* then how much from the read pointer onwards */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) fl = min(count, (size_t)lx->buffer_size - lx->lx_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) failed = copy_to_user(buff, lx->lx_buffer + lx->lx_read, fl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (failed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) /* and if there is anything left at the beginning of the buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (count - fl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) failed = copy_to_user(buff + fl, lx->lx_buffer, count - fl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) count -= failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) smp_wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) lx->lx_read = (lx->lx_read + count) % lx->buffer_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) smp_wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) mutex_unlock(&channel_wqs[index].mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) return count;
^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) ssize_t rtlx_write(int index, const void __user *buffer, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) struct rtlx_channel *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) unsigned long failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) size_t rt_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) size_t fl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) if (rtlx == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) rt = &rtlx->channel[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) mutex_lock(&channel_wqs[index].mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) smp_rmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) rt_read = rt->rt_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) /* total number of bytes to copy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) count = min_t(size_t, count, write_spacefree(rt_read, rt->rt_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) rt->buffer_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) /* first bit from write pointer to the end of the buffer, or count */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) fl = min(count, (size_t) rt->buffer_size - rt->rt_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) failed = copy_from_user(rt->rt_buffer + rt->rt_write, buffer, fl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (failed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) /* if there's any left copy to the beginning of the buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (count - fl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) failed = copy_from_user(rt->rt_buffer, buffer + fl, count - fl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) count -= failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) smp_wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) rt->rt_write = (rt->rt_write + count) % rt->buffer_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) smp_wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) mutex_unlock(&channel_wqs[index].mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) _interrupt_sp();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) static int file_open(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) return rtlx_open(iminor(inode), (filp->f_flags & O_NONBLOCK) ? 0 : 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) static int file_release(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return rtlx_release(iminor(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) static __poll_t file_poll(struct file *file, poll_table *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) int minor = iminor(file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) __poll_t mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) poll_wait(file, &channel_wqs[minor].rt_queue, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) poll_wait(file, &channel_wqs[minor].lx_queue, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (rtlx == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) /* data available to read? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) if (rtlx_read_poll(minor, 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) mask |= EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) /* space to write */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) if (rtlx_write_poll(minor))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) mask |= EPOLLOUT | EPOLLWRNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) return mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) static ssize_t file_read(struct file *file, char __user *buffer, size_t count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) int minor = iminor(file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) /* data available? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) if (!rtlx_read_poll(minor, (file->f_flags & O_NONBLOCK) ? 0 : 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) return 0; /* -EAGAIN makes 'cat' whine */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) return rtlx_read(minor, buffer, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) static ssize_t file_write(struct file *file, const char __user *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) int minor = iminor(file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) /* any space left... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (!rtlx_write_poll(minor)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) if (file->f_flags & O_NONBLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) ret = __wait_event_interruptible(channel_wqs[minor].rt_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) rtlx_write_poll(minor));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) return rtlx_write(minor, buffer, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) const struct file_operations rtlx_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) .open = file_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) .release = file_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) .write = file_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) .read = file_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) .poll = file_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) .llseek = noop_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) module_init(rtlx_module_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) module_exit(rtlx_module_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) MODULE_DESCRIPTION("MIPS RTLX");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) MODULE_LICENSE("GPL");