Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright 2010 2011 Mark Nelson and Tseng-Hui (Frank) Lin, IBM Corporation
0004  */
0005 
0006 #include <linux/errno.h>
0007 #include <linux/slab.h>
0008 #include <linux/export.h>
0009 #include <linux/irq.h>
0010 #include <linux/interrupt.h>
0011 #include <linux/of.h>
0012 #include <linux/list.h>
0013 #include <linux/notifier.h>
0014 
0015 #include <asm/machdep.h>
0016 #include <asm/rtas.h>
0017 #include <asm/irq.h>
0018 #include <asm/io_event_irq.h>
0019 
0020 #include "pseries.h"
0021 
0022 /*
0023  * IO event interrupt is a mechanism provided by RTAS to return
0024  * information about hardware error and non-error events. Device
0025  * drivers can register their event handlers to receive events.
0026  * Device drivers are expected to use atomic_notifier_chain_register()
0027  * and atomic_notifier_chain_unregister() to register and unregister
0028  * their event handlers. Since multiple IO event types and scopes
0029  * share an IO event interrupt, the event handlers are called one
0030  * by one until the IO event is claimed by one of the handlers.
0031  * The event handlers are expected to return NOTIFY_OK if the
0032  * event is handled by the event handler or NOTIFY_DONE if the
0033  * event does not belong to the handler.
0034  *
0035  * Usage:
0036  *
0037  * Notifier function:
0038  * #include <asm/io_event_irq.h>
0039  * int event_handler(struct notifier_block *nb, unsigned long val, void *data) {
0040  *  p = (struct pseries_io_event_sect_data *) data;
0041  *  if (! is_my_event(p->scope, p->event_type)) return NOTIFY_DONE;
0042  *      :
0043  *      :
0044  *  return NOTIFY_OK;
0045  * }
0046  * struct notifier_block event_nb = {
0047  *  .notifier_call = event_handler,
0048  * }
0049  *
0050  * Registration:
0051  * atomic_notifier_chain_register(&pseries_ioei_notifier_list, &event_nb);
0052  *
0053  * Unregistration:
0054  * atomic_notifier_chain_unregister(&pseries_ioei_notifier_list, &event_nb);
0055  */
0056 
0057 ATOMIC_NOTIFIER_HEAD(pseries_ioei_notifier_list);
0058 EXPORT_SYMBOL_GPL(pseries_ioei_notifier_list);
0059 
0060 static int ioei_check_exception_token;
0061 
0062 static char ioei_rtas_buf[RTAS_DATA_BUF_SIZE] __cacheline_aligned;
0063 
0064 /**
0065  * Find the data portion of an IO Event section from event log.
0066  * @elog: RTAS error/event log.
0067  *
0068  * Return:
0069  *  pointer to a valid IO event section data. NULL if not found.
0070  */
0071 static struct pseries_io_event * ioei_find_event(struct rtas_error_log *elog)
0072 {
0073     struct pseries_errorlog *sect;
0074 
0075     /* We should only ever get called for io-event interrupts, but if
0076      * we do get called for another type then something went wrong so
0077      * make some noise about it.
0078      * RTAS_TYPE_IO only exists in extended event log version 6 or later.
0079      * No need to check event log version.
0080      */
0081     if (unlikely(rtas_error_type(elog) != RTAS_TYPE_IO)) {
0082         printk_once(KERN_WARNING"io_event_irq: Unexpected event type %d",
0083                 rtas_error_type(elog));
0084         return NULL;
0085     }
0086 
0087     sect = get_pseries_errorlog(elog, PSERIES_ELOG_SECT_ID_IO_EVENT);
0088     if (unlikely(!sect)) {
0089         printk_once(KERN_WARNING "io_event_irq: RTAS extended event "
0090                 "log does not contain an IO Event section. "
0091                 "Could be a bug in system firmware!\n");
0092         return NULL;
0093     }
0094     return (struct pseries_io_event *) &sect->data;
0095 }
0096 
0097 /*
0098  * PAPR:
0099  * - check-exception returns the first found error or event and clear that
0100  *   error or event so it is reported once.
0101  * - Each interrupt returns one event. If a plateform chooses to report
0102  *   multiple events through a single interrupt, it must ensure that the
0103  *   interrupt remains asserted until check-exception has been used to
0104  *   process all out-standing events for that interrupt.
0105  *
0106  * Implementation notes:
0107  * - Events must be processed in the order they are returned. Hence,
0108  *   sequential in nature.
0109  * - The owner of an event is determined by combinations of scope,
0110  *   event type, and sub-type. There is no easy way to pre-sort clients
0111  *   by scope or event type alone. For example, Torrent ISR route change
0112  *   event is reported with scope 0x00 (Not Applicable) rather than
0113  *   0x3B (Torrent-hub). It is better to let the clients to identify
0114  *   who owns the event.
0115  */
0116 
0117 static irqreturn_t ioei_interrupt(int irq, void *dev_id)
0118 {
0119     struct pseries_io_event *event;
0120     int rtas_rc;
0121 
0122     for (;;) {
0123         rtas_rc = rtas_call(ioei_check_exception_token, 6, 1, NULL,
0124                     RTAS_VECTOR_EXTERNAL_INTERRUPT,
0125                     virq_to_hw(irq),
0126                     RTAS_IO_EVENTS, 1 /* Time Critical */,
0127                     __pa(ioei_rtas_buf),
0128                     RTAS_DATA_BUF_SIZE);
0129         if (rtas_rc != 0)
0130             break;
0131 
0132         event = ioei_find_event((struct rtas_error_log *)ioei_rtas_buf);
0133         if (!event)
0134             continue;
0135 
0136         atomic_notifier_call_chain(&pseries_ioei_notifier_list,
0137                        0, event);
0138     }
0139     return IRQ_HANDLED;
0140 }
0141 
0142 static int __init ioei_init(void)
0143 {
0144     struct device_node *np;
0145 
0146     ioei_check_exception_token = rtas_token("check-exception");
0147     if (ioei_check_exception_token == RTAS_UNKNOWN_SERVICE)
0148         return -ENODEV;
0149 
0150     np = of_find_node_by_path("/event-sources/ibm,io-events");
0151     if (np) {
0152         request_event_sources_irqs(np, ioei_interrupt, "IO_EVENT");
0153         pr_info("IBM I/O event interrupts enabled\n");
0154         of_node_put(np);
0155     } else {
0156         return -ENODEV;
0157     }
0158     return 0;
0159 }
0160 machine_subsys_initcall(pseries, ioei_init);
0161