xref: /openbmc/linux/drivers/misc/ibmasm/event.c (revision 4b4193256c8d3bc3a5397b5cd9494c2ad386317d)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds 
31da177e4SLinus Torvalds /*
41da177e4SLinus Torvalds  * IBM ASM Service Processor Device Driver
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Copyright (C) IBM Corporation, 2004
71da177e4SLinus Torvalds  *
8d36b6910SAl Viro  * Author: Max Asböck <amax@us.ibm.com>
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
11d43c36dcSAlexey Dobriyan #include <linux/sched.h>
125a0e3ad6STejun Heo #include <linux/slab.h>
131da177e4SLinus Torvalds #include "ibmasm.h"
14b8acb808SMax Asbock #include "lowlevel.h"
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds /*
171da177e4SLinus Torvalds  * ASM service processor event handling routines.
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  * Events are signalled to the device drivers through interrupts.
201da177e4SLinus Torvalds  * They have the format of dot commands, with the type field set to
211da177e4SLinus Torvalds  * sp_event.
221da177e4SLinus Torvalds  * The driver does not interpret the events, it simply stores them in a
231da177e4SLinus Torvalds  * circular buffer.
241da177e4SLinus Torvalds  */
251da177e4SLinus Torvalds 
wake_up_event_readers(struct service_processor * sp)261da177e4SLinus Torvalds static void wake_up_event_readers(struct service_processor *sp)
271da177e4SLinus Torvalds {
281da177e4SLinus Torvalds 	struct event_reader *reader;
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds 	list_for_each_entry(reader, &sp->event_buffer->readers, node)
311da177e4SLinus Torvalds                 wake_up_interruptible(&reader->wait);
321da177e4SLinus Torvalds }
331da177e4SLinus Torvalds 
34*63969cf2SLee Jones /*
351da177e4SLinus Torvalds  * receive_event
361da177e4SLinus Torvalds  * Called by the interrupt handler when a dot command of type sp_event is
371da177e4SLinus Torvalds  * received.
381da177e4SLinus Torvalds  * Store the event in the circular event buffer, wake up any sleeping
391da177e4SLinus Torvalds  * event readers.
401da177e4SLinus Torvalds  * There is no reader marker in the buffer, therefore readers are
413a4e3678SNick Andrew  * responsible for keeping up with the writer, or they will lose events.
421da177e4SLinus Torvalds  */
ibmasm_receive_event(struct service_processor * sp,void * data,unsigned int data_size)431da177e4SLinus Torvalds void ibmasm_receive_event(struct service_processor *sp, void *data, unsigned int data_size)
441da177e4SLinus Torvalds {
451da177e4SLinus Torvalds 	struct event_buffer *buffer = sp->event_buffer;
461da177e4SLinus Torvalds 	struct ibmasm_event *event;
471da177e4SLinus Torvalds 	unsigned long flags;
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds 	data_size = min(data_size, IBMASM_EVENT_MAX_SIZE);
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds 	spin_lock_irqsave(&sp->lock, flags);
521da177e4SLinus Torvalds 	/* copy the event into the next slot in the circular buffer */
531da177e4SLinus Torvalds 	event = &buffer->events[buffer->next_index];
54b8acb808SMax Asbock 	memcpy_fromio(event->data, data, data_size);
551da177e4SLinus Torvalds 	event->data_size = data_size;
561da177e4SLinus Torvalds 	event->serial_number = buffer->next_serial_number;
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds 	/* advance indices in the buffer */
591da177e4SLinus Torvalds 	buffer->next_index = (buffer->next_index + 1) % IBMASM_NUM_EVENTS;
601da177e4SLinus Torvalds 	buffer->next_serial_number++;
611da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sp->lock, flags);
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds 	wake_up_event_readers(sp);
641da177e4SLinus Torvalds }
651da177e4SLinus Torvalds 
event_available(struct event_buffer * b,struct event_reader * r)661da177e4SLinus Torvalds static inline int event_available(struct event_buffer *b, struct event_reader *r)
671da177e4SLinus Torvalds {
681da177e4SLinus Torvalds 	return (r->next_serial_number < b->next_serial_number);
691da177e4SLinus Torvalds }
701da177e4SLinus Torvalds 
71*63969cf2SLee Jones /*
721da177e4SLinus Torvalds  * get_next_event
731da177e4SLinus Torvalds  * Called by event readers (initiated from user space through the file
741da177e4SLinus Torvalds  * system).
751da177e4SLinus Torvalds  * Sleeps until a new event is available.
761da177e4SLinus Torvalds  */
ibmasm_get_next_event(struct service_processor * sp,struct event_reader * reader)771da177e4SLinus Torvalds int ibmasm_get_next_event(struct service_processor *sp, struct event_reader *reader)
781da177e4SLinus Torvalds {
791da177e4SLinus Torvalds 	struct event_buffer *buffer = sp->event_buffer;
801da177e4SLinus Torvalds 	struct ibmasm_event *event;
811da177e4SLinus Torvalds 	unsigned int index;
821da177e4SLinus Torvalds 	unsigned long flags;
831da177e4SLinus Torvalds 
84b8acb808SMax Asbock 	reader->cancelled = 0;
85b8acb808SMax Asbock 
86b8acb808SMax Asbock 	if (wait_event_interruptible(reader->wait,
87b8acb808SMax Asbock 			event_available(buffer, reader) || reader->cancelled))
881da177e4SLinus Torvalds 		return -ERESTARTSYS;
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds 	if (!event_available(buffer, reader))
911da177e4SLinus Torvalds 		return 0;
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds 	spin_lock_irqsave(&sp->lock, flags);
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds 	index = buffer->next_index;
961da177e4SLinus Torvalds 	event = &buffer->events[index];
971da177e4SLinus Torvalds 	while (event->serial_number < reader->next_serial_number) {
981da177e4SLinus Torvalds 		index = (index + 1) % IBMASM_NUM_EVENTS;
991da177e4SLinus Torvalds 		event = &buffer->events[index];
1001da177e4SLinus Torvalds 	}
1011da177e4SLinus Torvalds 	memcpy(reader->data, event->data, event->data_size);
1021da177e4SLinus Torvalds 	reader->data_size = event->data_size;
1031da177e4SLinus Torvalds 	reader->next_serial_number = event->serial_number + 1;
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sp->lock, flags);
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds 	return event->data_size;
1081da177e4SLinus Torvalds }
1091da177e4SLinus Torvalds 
ibmasm_cancel_next_event(struct event_reader * reader)110b8acb808SMax Asbock void ibmasm_cancel_next_event(struct event_reader *reader)
111b8acb808SMax Asbock {
112b8acb808SMax Asbock         reader->cancelled = 1;
113b8acb808SMax Asbock         wake_up_interruptible(&reader->wait);
114b8acb808SMax Asbock }
115b8acb808SMax Asbock 
ibmasm_event_reader_register(struct service_processor * sp,struct event_reader * reader)1161da177e4SLinus Torvalds void ibmasm_event_reader_register(struct service_processor *sp, struct event_reader *reader)
1171da177e4SLinus Torvalds {
1181da177e4SLinus Torvalds 	unsigned long flags;
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 	reader->next_serial_number = sp->event_buffer->next_serial_number;
1211da177e4SLinus Torvalds 	init_waitqueue_head(&reader->wait);
1221da177e4SLinus Torvalds 	spin_lock_irqsave(&sp->lock, flags);
1231da177e4SLinus Torvalds 	list_add(&reader->node, &sp->event_buffer->readers);
1241da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sp->lock, flags);
1251da177e4SLinus Torvalds }
1261da177e4SLinus Torvalds 
ibmasm_event_reader_unregister(struct service_processor * sp,struct event_reader * reader)1271da177e4SLinus Torvalds void ibmasm_event_reader_unregister(struct service_processor *sp, struct event_reader *reader)
1281da177e4SLinus Torvalds {
1291da177e4SLinus Torvalds 	unsigned long flags;
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 	spin_lock_irqsave(&sp->lock, flags);
1321da177e4SLinus Torvalds 	list_del(&reader->node);
1331da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sp->lock, flags);
1341da177e4SLinus Torvalds }
1351da177e4SLinus Torvalds 
ibmasm_event_buffer_init(struct service_processor * sp)1361da177e4SLinus Torvalds int ibmasm_event_buffer_init(struct service_processor *sp)
1371da177e4SLinus Torvalds {
1381da177e4SLinus Torvalds 	struct event_buffer *buffer;
1391da177e4SLinus Torvalds 	struct ibmasm_event *event;
1401da177e4SLinus Torvalds 	int i;
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 	buffer = kmalloc(sizeof(struct event_buffer), GFP_KERNEL);
1431da177e4SLinus Torvalds 	if (!buffer)
144f403f85fSAnton Vasilyev 		return -ENOMEM;
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 	buffer->next_index = 0;
1471da177e4SLinus Torvalds 	buffer->next_serial_number = 1;
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 	event = buffer->events;
1501da177e4SLinus Torvalds 	for (i=0; i<IBMASM_NUM_EVENTS; i++, event++)
1511da177e4SLinus Torvalds 		event->serial_number = 0;
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds 	INIT_LIST_HEAD(&buffer->readers);
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 	sp->event_buffer = buffer;
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 	return 0;
1581da177e4SLinus Torvalds }
1591da177e4SLinus Torvalds 
ibmasm_event_buffer_exit(struct service_processor * sp)1601da177e4SLinus Torvalds void ibmasm_event_buffer_exit(struct service_processor *sp)
1611da177e4SLinus Torvalds {
1621da177e4SLinus Torvalds 	kfree(sp->event_buffer);
1631da177e4SLinus Torvalds }
164