powerpc, powernv: Add OPAL platform event driver
PATCH v3 1/1 powerpc, powernv: Add OPAL platform event driver
This patch creates a new OPAL platform event character driver which will give userspace clients the access to these events and process them effectively. Following platforms events are currently supported with this platform driver.
(1) Environmental and Power Warning (EPOW)
(2) Delayed Power Off (DPO)
The user interface for this driver is /dev/opal_event character device file where the user space clients can poll and read for new opal platform events. The expected sequence of events driven from user space should be like the following.
(1) Open the character device file
(2) Poll on the file for POLLIN event
(3) When unblocked, must attempt to read PLAT_EVENT_MAX_SIZE size
(4) Kernel driver will pass at most one opal_plat_event structure
(5) Poll again for more new events
The driver registers for OPAL messages notifications corresponding to individual OPAL events. When any of those event messages arrive in the kernel, the callbacks are called to process them which in turn unblocks the polling thread on the character device file. The driver also registers a timer function which will be called after a threshold amount of time to shutdown the system. The user space client receives the timeout value for all individual OPAL platform events and hence must prepare the system and eventually shutdown. In case the user client does not shutdown the system, the timer function will be called after the threshold and shutdown the system explicitly.
---
Changes in V3:
- Rebased the patch against the mainline
Changes in V2:
- Changed the function fetch_dpo_timeout
- Export opal_platform_events.h for user space consumption
- Posted here https://patchwork.ozlabs.org/patch/396725/
Original V1:
- Original patch
- Posted here http://patchwork.ozlabs.org/patch/394340/
arch/powerpc/include/asm/opal.h | 45 +- arch/powerpc/include/uapi/asm/Kbuild | 1 + .../include/uapi/asm/opal_platform_events.h | 90 +++ arch/powerpc/platforms/powernv/Makefile | 2 +- .../platforms/powernv/opal-platform-events.c | 736 +++++++++++++++++++++ arch/powerpc/platforms/powernv/opal-wrappers.S | 1 + arch/powerpc/platforms/powernv/opal.c | 8 +- 7 files changed, 880 insertions(+), 3 deletions(-) create mode 100644 arch/powerpc/include/uapi/asm/opal_platform_events.h create mode 100644 arch/powerpc/platforms/powernv/opal-platform-events.c
arch/powerpc/include/asm/opal.h +44 -1
@@ -154,6 +154,7 @@ struct opal_sg_list {#define OPAL_HANDLE_HMI 98#define OPAL_REGISTER_DUMP_REGION 101#define OPAL_UNREGISTER_DUMP_REGION 102 +#define OPAL_GET_DPO_STATUS 105#ifndef __ASSEMBLY__
@@ -281,6 +282,7 @@ enum OpalMessageType {OPAL_MSG_EPOW ,OPAL_MSG_SHUTDOWN ,OPAL_MSG_HMI_EVT , +OPAL_MSG_DPO , OPAL_MSG_TYPE_MAX, };
@@ -452,6 +454,46 @@ struct opal_msg {__be64 params[8 ]; }; +/* +* EPOW status sharing (OPAL and the host) +* +* The host will pass on OPAL, a buffer of length OPAL_SYSEPOW_MAX +* with individual elements being 16 bits wide to fetch the system +* wide EPOW status. Each element in the buffer will contain the +* EPOW status in it's bit representation for a particular EPOW sub +* class as defiend here. So multiple detailed EPOW status bits +* specific for any sub class can be represented in a single buffer +* element as it's bit representation. +*/ + +/* System EPOW type */ +enum OpalSysEpow { +OPAL_SYSEPOW_POWER =0 ,/* Power EPOW */ +OPAL_SYSEPOW_TEMP =1 ,/* Temperature EPOW */ +OPAL_SYSEPOW_COOLING =2 ,/* Cooling EPOW */ +OPAL_SYSEPOW_MAX =3 ,/* Max EPOW categories */ +}; + +/* Power EPOW */ +enum OpalSysPower { +OPAL_SYSPOWER_UPS =0x0001 ,/* System on UPS power */ +OPAL_SYSPOWER_CHNG =0x0002 ,/* System power config change */ +OPAL_SYSPOWER_FAIL =0x0004 ,/* System impending power failure */ +OPAL_SYSPOWER_INCL =0x0008 ,/* System incomplete power */ +}; + +/* Temperature EPOW */ +enum OpalSysTemp { +OPAL_SYSTEMP_AMB =0x0001 ,/* System over ambient temperature */ +OPAL_SYSTEMP_INT =0x0002 ,/* System over internal temperature */ +OPAL_SYSTEMP_HMD =0x0004 ,/* System over ambient humidity */ +}; + +/* Cooling EPOW */ +enum OpalSysCooling { +OPAL_SYSCOOL_INSF =0x0001 ,/* System insufficient cooling */ +}; +struct opal_machine_check_event {enum OpalMCE_Version version:8 ;/* 0x00 */ uint8_t in_use;/* 0x01 */
@@ -918,7 +960,7 @@ int64_t opal_pci_fence_phb(uint64_t phb_id);int64_t opal_pci_reinit (uint64_t phb_id,uint64_t reinit_scope,uint64_t data);int64_t opal_pci_mask_pe_error (uint64_t phb_id,uint16_t pe_number,uint8_t error_type,uint8_t mask_action);int64_t opal_set_slot_led_status (uint64_t phb_id,uint64_t slot_id,uint8_t led_type,uint8_t led_action); -int64_t opal_get_epow_status (__be64 *status); +int64_t opal_get_epow_status (uint16_t *status,uint16_t *length);int64_t opal_set_system_attention_led (uint8_t led_action);int64_t opal_pci_next_error (uint64_t phb_id,__be64 *first_frozen_pe,__be16 *pci_error_type,__be16 *severity);
@@ -963,6 +1005,7 @@ int64_t opal_handle_hmi(void);int64_t opal_register_dump_region (uint32_t id,uint64_t start,uint64_t end);int64_t opal_unregister_dump_region (uint32_t id);int64_t opal_pci_set_phb_cxl_mode (uint64_t phb_id,uint64_t mode,uint64_t pe_number); +int64_t opal_get_dpo_status (int64_t *dpo_timeout);/* Internal functions */ extern int early_init_dt_scan_opal(unsigned long node, const char *uname,
arch/powerpc/include/uapi/asm/Kbuild +1 -0
@@ -44,3 +44,4 @@ header-y += tm.h header-y += types.h header-y += ucontext.h header-y += unistd.h +header-y += opal_platform_events.h
arch/powerpc/include/uapi/asm/opal_platform_events.h new file +90 -0
90 lines not on this page · raw message
arch/powerpc/platforms/powernv/Makefile +1 -1
@@ -1,7 +1,7 @@ obj-y += setup.o opal-wrappers.o opal.o opal-async.o obj-y += opal-rtc.o opal-nvram.o opal-lpc.o opal-flash.o obj-y += rng.o opal-elog.o opal-dump.o opal-sysparam.o opal-sensor.o -obj-y += opal-msglog.o opal-hmi.o +obj-y += opal-msglog.o opal-hmi.o opal-platform-events.o obj-$(CONFIG_SMP) += smp.o subcore.o subcore-asm.o obj-$(CONFIG_PCI) += pci.o pci-p5ioc2.o pci-ioda.o
arch/powerpc/platforms/powernv/opal-platform-events.c new file +736 -0
736 lines not on this page · raw message
arch/powerpc/platforms/powernv/opal-wrappers.S +1 -0
@@ -250,3 +250,4 @@ OPAL_CALL(opal_handle_hmi, OPAL_HANDLE_HMI); OPAL_CALL(opal_register_dump_region, OPAL_REGISTER_DUMP_REGION); OPAL_CALL(opal_unregister_dump_region, OPAL_UNREGISTER_DUMP_REGION); OPAL_CALL(opal_pci_set_phb_cxl_mode, OPAL_PCI_SET_PHB_CXL_MODE); +OPAL_CALL(opal_get_dpo_status, OPAL_GET_DPO_STATUS);
arch/powerpc/platforms/powernv/opal.c +7 -1
@@ -646,7 +646,7 @@ static void __init opal_dump_region_init(void) }static int __init opal_init (void ) { -struct device_node *np, *consoles; +struct device_node *np, *consoles, *epow;const __be32 *irqs;int rc, i, irqlen;
@@ -670,6 +670,12 @@ static int __init opal_init(void)of_node_put (consoles); } + epow =of_find_node_by_path ("/ibm,opal/epow" ); +if (epow) { +of_platform_device_create (epow,"opal_event" ,NULL ); +of_node_put (epow); + } +/* Find all OPAL interrupts and request them */ irqs =of_get_property (opal_node,"opal-interrupts" , &irqlen); pr_debug("opal: Found %d interrupts reserved for OPAL\n",
Signature · 1 line
1.9.3
Quoted 36 lines · Anshuman Khandual
This patch creates a new OPAL platform event character driver which will give userspace clients the access to these events and process them effectively. Following platforms events are currently supported with this platform driver.
(1) Environmental and Power Warning (EPOW)
(2) Delayed Power Off (DPO)The user interface for this driver is
/dev/opal_eventcharacter device file where the user space clients can poll and read for new opal platform events. The expected sequence of events driven from user space should be like the following.(1) Open the character device file
(2) Poll on the file for POLLIN event
(3) When unblocked, must attempt to readPLAT_EVENT_MAX_SIZEsize
(4) Kernel driver will pass at most oneopal_plat_eventstructure
(5) Poll again for more new eventsThe driver registers for OPAL messages notifications corresponding to individual OPAL events. When any of those event messages arrive in the kernel, the callbacks are called to process them which in turn unblocks the polling thread on the character device file. The driver also registers a timer function which will be called after a threshold amount of time to shutdown the system. The user space client receives the timeout value for all individual OPAL platform events and hence must prepare the system and eventually shutdown. In case the user client does not shutdown the system, the timer function will be called after the threshold and shutdown the system explicitly.
Reviewed-by Neelesh Gupta <[email protected]>Signed-off-by Anshuman Khandual <[email protected]>---
Changes in V3:
- Rebased the patch against the mainline
Quoted 8 lines
Changes in V2:
- Changed the functionfetch_dpo_timeout
- Exportopal_platform_events.hfor user space consumption
- Posted here https://patchwork.ozlabs.org/patch/396725/Original V1:
- Original patch
- Posted here http://patchwork.ozlabs.org/patch/394340/
Hey Michael,
Do you have any updates on this patch yet ? Thanks !
Hi Anshuman,
Quoted 13 lines
This patch creates a new OPAL platform event character driver which will give userspace clients the access to these events and process them effectively. Following platforms events are currently supported with this platform driver.
(1) Environmental and Power Warning (EPOW)
(2) Delayed Power Off (DPO)The user interface for this driver is
/dev/opal_eventcharacter device file where the user space clients can poll and read for new opal platform events. The expected sequence of events driven from user space should be like the following.
Quoted 5 lines
(1) Open the character device file
(2) Poll on the file for POLLIN event
(3) When unblocked, must attempt to readPLAT_EVENT_MAX_SIZEsize
(4) Kernel driver will pass at most oneopal_plat_eventstructure
(5) Poll again for more new events
Why is the poll() required? Can't read() just block if there are no events (and !O_NONBLOCK, of course).
arch/powerpc/include/asm/opal.h enum OpalMessageType { · line 287
Quoted 7 lines
arch/powerpc/include/asm/opal.h +1
-0@@ -281,6 +282,7 @@ enum OpalMessageType {OPAL_MSG_EPOW ,OPAL_MSG_SHUTDOWN ,OPAL_MSG_HMI_EVT , +OPAL_MSG_DPO , OPAL_MSG_TYPE_MAX, };
I'd suggest adding the DPO interface definitions as a separate change, but that's no huge deal.
Quoted 6 lines
arch/powerpc/include/asm/opal.h +1
-0@@ -452,6 +454,46 @@ struct opal_msg {__be64 params[8 ]; }; +/*
arch/powerpc/include/asm/opal.h struct opal_msg { · line 467
Quoted 10 lines
arch/powerpc/include/asm/opal.h +10
-0+* EPOW status sharing (OPAL and the host) +* +* The host will pass on OPAL, a buffer of length OPAL_SYSEPOW_MAX +* with individual elements being 16 bits wide to fetch the system +* wide EPOW status. Each element in the buffer will contain the +* EPOW status in it's bit representation for a particular EPOW sub +* class as defiend here. So multiple detailed EPOW status bits +* specific for any sub class can be represented in a single buffer +* element as it's bit representation. +*/
Why do we have this in separate u16s? This just results in a really sparse bitmask.
I guess the FW interface is already defined though, right?
[Minor grammar nit: "it's" is short for "it is", which isn't what you want here :)]
arch/powerpc/include/asm/opal.h int64_t opal_pci_fence_phb(uint64_t phb_id); · line 963
Quoted 2 lines
arch/powerpc/include/asm/opal.h +1
-1-int64_t opal_get_epow_status (__be64 *status); +int64_t opal_get_epow_status (uint16_t *status,uint16_t *length);
Please keep the endian annotations there.
arch/powerpc/include/asm/opal.h int64_t opal_handle_hmi(void); · line 1008
Quoted 1 line
arch/powerpc/include/asm/opal.h +1
-0+int64_t opal_get_dpo_status (int64_t *dpo_timeout);
and __be64 * here.
Quoted 7 lines
arch/powerpc/include/uapi/asm/opal_platform_events.h new file +7
-0+ +/* EPOW classification */ +enum epow_condition { +EPOW_SYSPOWER_CHNG =0 ,/* Power */ +EPOW_SYSPOWER_FAIL =1 , +EPOW_SYSPOWER_INCL =2 , +EPOW_SYSPOWER_UPS =3 ,
arch/powerpc/include/uapi/asm/opal_platform_events.h line 39
Quoted 10 lines
arch/powerpc/include/uapi/asm/opal_platform_events.h new file +10
-0+EPOW_SYSTEMP_AMB =4 ,/* Temperature */ +EPOW_SYSTEMP_INT =5 , +EPOW_SYSTEMP_HMD =6 , +EPOW_SYSCOOL_INSF =7 ,/* Cooling */ + EPOW_MAX = 8, +}; + +/* OPAL EPOW event */ +struct epow_event { + __u64 epow[EPOW_MAX]; /* Detailed system EPOW status */
Why an array of u64s when you're only using one bit of each? Do we expect other status details to be set in each of these?
If not, you're converting from one sparse bitmask (the OPAL interface) to another different sparse bitmask (the kernel interface). Either keep them the same, or use a more sensible format.
Also, are you expecting a single epow_event to have multiple conditons set? If not, no need for a bitmask at all.
arch/powerpc/include/uapi/asm/opal_platform_events.h line 47
Quoted 8 lines
arch/powerpc/include/uapi/asm/opal_platform_events.h new file +8
-0+__u64 timeout;/* Timeout to shutdown in secs */ +}; + +/* OPAL DPO event */ +struct dpo_event { +__u64 orig_timeout;/* Platform provided timeout in secs */ +__u64 remain_timeout;/* Timeout to shutdown in secs */ +};
Will all of these events have a timeout? If so, just put a timeout in struct opal_plat_event.
Why does the DPO timeout have orig/remain timeouts, but not the EPOW one?
See below, but perhaps we want a absolute timeout here, instead of relative? (which is essentially invalid as soon as you've read it).
Quoted 14 lines
arch/powerpc/include/uapi/asm/opal_platform_events.h new file +14
-0+ +/* OPAL event */ +struct opal_plat_event { +__u32 type;/* Type of OPAL platform event */ +#define OPAL_PLAT_EVENT_TYPE_EPOW 0 +#define OPAL_PLAT_EVENT_TYPE_DPO 1 +#define OPAL_PLAT_EVENT_TYPE_MAX 2 +__u32 size;/* Size of OPAL platform event */ +union { +struct epow_event epow;/* EPOW platform event */ +struct dpo_event dpo;/* DPO platform event */ + }; +}; +
arch/powerpc/include/uapi/asm/opal_platform_events.h line 71
Quoted 10 lines
arch/powerpc/include/uapi/asm/opal_platform_events.h new file +10
-0+/* +* Suggested read size +* +* The user space client should attempt to read OPAL_PLAT_EVENT_READ_SIZE +* amount of data from the character device file '/dev/opal_event' at any +* point of time. The kernel driver will pass an entire opal_plat_event +* structure in every read. This ensures that minium data the user space +* client gets from the kernel is one opal_plat_event structure. +*/ +#define PLAT_EVENT_MAX_SIZE 4096
-> OPAL_PLAT_EVENT_MAX_SIZE - this it a global header.
Quoted 5 lines
arch/powerpc/platforms/powernv/opal-platform-events.c new file +5
-0+/* +* opal_event_read +* +* User client needs to attempt to read PLAT_EVENT_MAX_SIZE amount of data +* from the file descriptor at a time. The driver will pass a single node
arch/powerpc/platforms/powernv/opal-platform-events.c line 154
Quoted 10 lines
arch/powerpc/platforms/powernv/opal-platform-events.c new file +10
-0+* from the list if available at a time and then delete the node from the list. +*/ +static ssize_t opal_event_read (struct file *filep, +char __user *buf,size_t len,loff_t *off) +{ +struct opal_platform_evt *evt; +unsigned long flags; + +if (len <sizeof (struct opal_plat_event )) +return -EINVAL ;
Maybe -EMSGSIZE?
Quoted 16 lines
arch/powerpc/platforms/powernv/opal-platform-events.c new file +16
-0+ +/* Fetch the first node on the list */ +spin_lock_irqsave (&opal_plat_evt_spinlock, flags); +if (list_empty (&opal_event_queue)) { +spin_unlock_irqrestore (&opal_plat_evt_spinlock, flags); +return 0 ; + } + +/* Fetch and delete from the list */ + evt =list_first_entry (&opal_event_queue, + struct opal_platform_evt, link); +list_del (&evt->link); + +/* +* Update the remaining timeout for DPO event. +* This can only be updated during the read time.
arch/powerpc/platforms/powernv/opal-platform-events.c line 180
Quoted 10 lines
arch/powerpc/platforms/powernv/opal-platform-events.c new file +10
-0+*/ +if (evt->opal_event.type ==OPAL_PLAT_EVENT_TYPE_DPO ) { +unsigned long timeout; + +if (opal_dpo_target && + evt->opal_event.dpo.orig_timeout) { + timeout = (opal_dpo_target - jiffies) /HZ ; + evt->opal_event.dpo.remain_timeout = timeout; + } + }
Would it make more sense to just put one timestamp in here (when we first see the event from OPAL), of the actual timeout? What's the global opal_dpo_target for? Doesn't that just mean subsequent DPO events will invalidate others' timeouts?
Also, do you need the spinlock held here? You've already dequeued the item from the list. If you keep the opal_dpo_target global, you'll need to do updates with the lock held too.
Quoted 43 lines
arch/powerpc/platforms/powernv/opal-platform-events.c new file +43
-0+spin_unlock_irqrestore (&opal_plat_evt_spinlock, flags); + +if (copy_to_user (buf, &evt->opal_event, +sizeof (struct opal_plat_event ))) { + +/* +* Copy to user has failed. The event node had +* been deleted from the list. Lets add it back +* there. +*/ +spin_lock_irqsave (&opal_plat_evt_spinlock, flags); +list_add_tail (&evt->link, &opal_event_queue); +spin_unlock_irqrestore (&opal_plat_evt_spinlock, flags); +return -EFAULT ; + } + +kfree (evt); +return sizeof (struct opal_plat_event ); +} + +/* +* opal_event_poll +* +* Poll is unblocked right away with POLLIN when data is available. +* When data is not available, the process will have to block till +* it gets waked up and data is available to read. +*/ +static unsigned int opal_event_poll (struct file *file,poll_table *wait) +{ +poll_wait (file, &opal_plat_evt_wait, wait); +if (!list_empty (&opal_event_queue)) +return POLLIN ; +return 0 ; +} + +/* +* opal_event_open +* +* This makes sure that only one process can open the +* character device file at any point of time. Others +* attempting to open the file descriptor will either +* get EBUSY (with O_NONBLOCK flag) or wait for the +* other process to close the file descriptor.
arch/powerpc/platforms/powernv/opal-platform-events.c line 233
Quoted 10 lines
arch/powerpc/platforms/powernv/opal-platform-events.c new file +10
-0+*/ +static int opal_event_open (struct inode *inode,struct file *file) +{ +int err; + +mutex_lock (&opal_plat_evt_mutex); +while (opal_event_open_flag) { +mutex_unlock (&opal_plat_evt_mutex); +if (file->f_flags &O_NONBLOCK ) +return -EBUSY ;
O_NONBLOCK would signify that all subsequent operations are non-blocking, not just the open. It happens that this is the case (because of your requirement on poll()), but it shouldn't be.
Just return -EBUSY, and get rid of the wait queue.
Quoted 61 lines
arch/powerpc/platforms/powernv/opal-platform-events.c new file +61
-0+/* Process the received EPOW information */ +void process_epow (__u64 *epow,int16_t *epow_status,int max_epow_class) +{ +/* +* Platform might have returned less number of EPOW +* subclass status than asked for. This situation +* happens when the platform firmware is older compared +* to the kernel. +*/ + +if (!max_epow_class) { +pr_warn ("EPOW: OPAL_SYSEPOW_POWER subclass not present\n" ); +return ; + } + +/* Power */ + max_epow_class--; +if (epow_status[OPAL_SYSEPOW_POWER ] &OPAL_SYSPOWER_CHNG ) { +pr_info ("EPOW: Power configuration changed\n" ); + epow[EPOW_SYSPOWER_CHNG ] =1 ; + } + +if (epow_status[OPAL_SYSEPOW_POWER ] &OPAL_SYSPOWER_FAIL ) { +pr_info ("EPOW: Impending system power failure\n" ); + epow[EPOW_SYSPOWER_FAIL ] =1 ; + } + +if (epow_status[OPAL_SYSEPOW_POWER ] &OPAL_SYSPOWER_INCL ) { +pr_info ("EPOW: Incomplete system power\n" ); + epow[EPOW_SYSPOWER_INCL ] =1 ; + } + +if (epow_status[OPAL_SYSEPOW_POWER ] &OPAL_SYSPOWER_UPS ) { +pr_info ("EPOW: System on UPS power\n" ); + epow[EPOW_SYSPOWER_UPS ] =1 ; + } + +if (!max_epow_class) { +pr_warn ("EPOW: OPAL_SYSEPOW_TEMP subclass not present\n" ); +return ; + } + +/* Temperature */ + max_epow_class--; +if (epow_status[OPAL_SYSEPOW_TEMP ] &OPAL_SYSTEMP_AMB ) { +pr_info ("EPOW: Over ambient temperature\n" ); + epow[EPOW_SYSTEMP_AMB ] =1 ; + } + +if (epow_status[OPAL_SYSEPOW_TEMP ] &OPAL_SYSTEMP_INT ) { +pr_info ("EPOW: Over internal temperature\n" ); + epow[EPOW_SYSTEMP_INT ] =1 ; + } + +if (epow_status[OPAL_SYSEPOW_TEMP ] &OPAL_SYSTEMP_HMD ) { +pr_info ("EPOW: Over internal humidity\n" ); + epow[EPOW_SYSTEMP_HMD ] =1 ; + } + +if (!max_epow_class) { +pr_warn ("EPOW: OPAL_SYSEPOW_COOLING subclass not present\n" );
arch/powerpc/platforms/powernv/opal-platform-events.c line 340
Quoted 10 lines
arch/powerpc/platforms/powernv/opal-platform-events.c new file +10
-0+return ; + } + +/* Cooling */ + max_epow_class--; +if (epow_status[OPAL_SYSEPOW_COOLING ] &OPAL_SYSCOOL_INSF ) { +pr_info ("EPOW: Insufficient cooling\n" ); + epow[EPOW_SYSCOOL_INSF ] =1 ; + } +}
This is fairly verbose, due to the separate u64s in the epow types.
Quoted 13 lines
arch/powerpc/platforms/powernv/opal-platform-events.c new file +13
-0+ +/* +* fetch_epow_status +* +* Fetch the system EPOW status through an OPAL call and +* validate the number of EPOW sub class status received. +*/ +static void fetch_epow_status (int16_t *epow_status,int16_t *n_epow) +{ +int rc; + +memset (epow_status,0 ,sizeof (int16_t ) *OPAL_SYSEPOW_MAX ); + *n_epow =OPAL_SYSEPOW_MAX ;
arch/powerpc/platforms/powernv/opal-platform-events.c line 363
Quoted 10 lines
arch/powerpc/platforms/powernv/opal-platform-events.c new file +10
-0+ rc =opal_get_epow_status (epow_status, n_epow); +if (rc !=OPAL_SUCCESS ) { +pr_err ("EPOW: OPAL call failed\n" ); +memset (epow_status,0 ,sizeof (int16_t ) *OPAL_SYSEPOW_MAX ); + *n_epow =0 ; +return ; + } +if (!(*n_epow)) +pr_err ("EPOW: No subclass status received\n" ); +}
Endian conversion?
Quoted 6 lines
arch/powerpc/platforms/powernv/opal-platform-events.c new file +6
-0+ +/* +* fetch_dpo_timeout +* +* Fetch the system DPO timeout status through an OPAL call. +*/
arch/powerpc/platforms/powernv/opal-platform-events.c line 379
Quoted 10 lines
arch/powerpc/platforms/powernv/opal-platform-events.c new file +10
-0+static void fetch_dpo_timeout (int64_t *dpo_timeout) +{ +int rc; + + rc =opal_get_dpo_status (dpo_timeout); +if (rc ==OPAL_WRONG_STATE ) { +pr_info ("DPO: Not initiated by OPAL\n" ); + *dpo_timeout =0 ; + } +}
And here too?
Quoted 22 lines
arch/powerpc/platforms/powernv/opal-platform-events.c new file +22
-0+ +/* +* valid_epow +* +* Validate the received EPOW event status. This ensures +* that there are valid status for various EPOW sub classes +* and their individual events. +*/ +static bool valid_epow (int16_t *epow_status,int16_t n_epow) +{ +int i; + +/* EPOW sub classes present */ +if (!n_epow) +return false; + +/* EPOW events present */ +for (i =0 ; i < n_epow; i++) { +if (epow_status[i]) +return true; + } +return false;
arch/powerpc/platforms/powernv/opal-platform-events.c line 411
Quoted 10 lines
arch/powerpc/platforms/powernv/opal-platform-events.c new file +10
-0+} + +/* +* epow_exclude +* +* XXX: EPOW events on the action exclude list. System shutdown +* would not be scheduled for all these platform events. In future +* this should be communicated from the platform firmware through +* device tree attributes. +*/
Exclude from what? System shutdown, right? You have the logic between this and the subsequent function reversed, which makes it a little complex to red.
Maybe make this epow_event_is_actionable()?
Quoted 115 lines
arch/powerpc/platforms/powernv/opal-platform-events.c new file +115
-0+/* +* opal_event_handle_basic +* +* Sets up the basic information for an opal platform event, +* activates the timer, adds to the list and wakes up waiting +* threads on the character device. +*/ +static void opal_event_handle_basic (struct opal_platform_evt *evt, +unsigned long type,unsigned long timeout) +{ +unsigned long flags; + + evt->opal_event.type = type; +switch (type) { +case OPAL_PLAT_EVENT_TYPE_EPOW : + evt->opal_event.size =sizeof (struct epow_event ); + evt->opal_event.epow.timeout = timeout; +if (actionable_epow (evt->opal_event.epow.epow)) +opal_event_start_timer (OPAL_PLAT_EVENT_TYPE_EPOW , +OPAL_EPOW_TIMEOUT ); +break ; +case OPAL_PLAT_EVENT_TYPE_DPO : + evt->opal_event.size =sizeof (struct dpo_event ); + evt->opal_event.dpo.orig_timeout = timeout; +opal_event_start_timer (OPAL_PLAT_EVENT_TYPE_DPO , timeout); +break ; +default : +pr_err ("Unknown event type\n" ); +break ; + } +spin_lock_irqsave (&opal_plat_evt_spinlock, flags); +list_add_tail (&evt->link, &opal_event_queue); +spin_unlock_irqrestore (&opal_plat_evt_spinlock, flags); +wake_up_interruptible (&opal_plat_evt_wait); +} + +/* +* opal_event_existing_status +* +* Fetch and process existing opal platform event conditions +* present on the system. If events detected, add them to the +* list which can be consumed by the user space right away. +*/ +static void opal_event_existing_status (void ) +{ +struct opal_platform_evt *evt; +int64_t dpo_timeout; +int16_t epow_status[OPAL_SYSEPOW_MAX ], n_epow; + +fetch_epow_status (epow_status, &n_epow); +if (valid_epow (epow_status, n_epow)) { + evt =kzalloc (sizeof (struct opal_platform_evt ),GFP_KERNEL ); +if (!evt) { +pr_err ("EPOW: Memory allocation for event failed\n" ); +return ; + } +process_epow (evt->opal_event.epow.epow, epow_status, n_epow); +opal_event_handle_basic (evt,OPAL_PLAT_EVENT_TYPE_EPOW , +OPAL_EPOW_TIMEOUT ); + } + +fetch_dpo_timeout (&dpo_timeout); +if (dpo_timeout) { + evt =kzalloc (sizeof (struct opal_platform_evt ),GFP_KERNEL ); +if (!evt) { +pr_err ("DPO: Memory allocation for event failed\n" ); +return ; + } + opal_dpo_target = jiffies + dpo_timeout *HZ ; +opal_event_handle_basic (evt,OPAL_PLAT_EVENT_TYPE_DPO , + dpo_timeout); + } +} + +/* Platform EPOW message received */ +static int opal_epow_event (struct notifier_block *nb, +unsigned long msg_type,void *msg) +{ +struct opal_platform_evt *evt; +int16_t epow_status[OPAL_SYSEPOW_MAX ], n_epow; + +if (msg_type !=OPAL_MSG_EPOW ) +return 0 ; + +fetch_epow_status (epow_status, &n_epow); +if (!valid_epow (epow_status, n_epow)) +return -EINVAL ; + +pr_debug ("EPOW event: Power(%x) Thermal(%x) Cooling(%x)\n" , + epow_status[0 ], epow_status[1 ], epow_status[2 ]); + evt =kzalloc (sizeof (struct opal_platform_evt ),GFP_KERNEL ); +if (!evt) { +pr_err ("EPOW: Memory allocation for event failed\n" ); +return -ENOMEM ; + } +process_epow (evt->opal_event.epow.epow, epow_status, n_epow); +opal_event_handle_basic (evt, +OPAL_PLAT_EVENT_TYPE_EPOW ,OPAL_EPOW_TIMEOUT ); +return 0 ; +} + +/* Platform DPO message received */ +static int opal_dpo_event (struct notifier_block *nb, +unsigned long msg_type,void *msg) +{ +struct opal_platform_evt *evt; +int64_t dpo_timeout; + +if (msg_type !=OPAL_MSG_DPO ) +return 0 ; + +fetch_dpo_timeout (&dpo_timeout); +if (!dpo_timeout) +return -EINVAL ; +
arch/powerpc/platforms/powernv/opal-platform-events.c line 574
Quoted 10 lines
arch/powerpc/platforms/powernv/opal-platform-events.c new file +10
-0+pr_debug ("DPO event: Timeout:%llu\n" , dpo_timeout); + evt =kzalloc (sizeof (struct opal_platform_evt ),GFP_KERNEL ); +if (!evt) { +pr_err ("DPO: Memory allocation for event failed\n" ); +return -ENOMEM ; + } + opal_dpo_target = jiffies + dpo_timeout *HZ ; +opal_event_handle_basic (evt,OPAL_PLAT_EVENT_TYPE_DPO , dpo_timeout); +return 0 ; +}
Okay, these are a little messy:
1) an inocoming EPOW/DPO event will invoke both notifier callbacks
2) the notifier callback will check if it's the right handler for the msg_type, and handle the event, so we get separate code paths for EPOW & DPO.
3) both callbacks then converge on a single code path, by calling opal_event_basic with a specific type argument. Here we get a single code path for EPOW & DPO.
4) opal_event_basic then checks the type, and performs separate functions depending on EPOW vs. DPO
- and then the logic (and code) is repeated for the opal_existing_event_status function
Can you unify this, so there's less duplication and conditional code?
Also, are you sure you can kzalloc(GFP_KERNEL) here?
Quoted 37 lines
arch/powerpc/platforms/powernv/opal-platform-events.c new file +36
-0+/* Platform driver probe */ +static int opal_event_probe (struct platform_device *pdev) +{ +struct device *dev; +int ret; + +if (opal_event_probe_finished) { +pr_err ("%s getting called once again\n" , __func__); +return 0 ; + } + opal_event_probe_finished = true; + +init_timer (&opal_event_timer); + opal_event_timer.function = opal_event_timeout; + opal_event_open_flag = false; + opal_dpo_target =0 ; + + ret =alloc_chrdev_region (&opal_event_dev,0 , +OPAL_EVENT_MAX_DEVS ,"opal_event" ); +if (ret <0 ) { +dev_err (&pdev->dev,"aloc_chrdev_region failed\n" ); +return ret; + } + + opal_event_class =class_create (THIS_MODULE ,"opal_event" ); +if (IS_ERR (opal_event_class)) { + ret =PTR_ERR (opal_event_class); +dev_err (&pdev->dev,"class_create failed with %d\n" , ret); + goto fail_chrdev; + } + + dev =device_create (opal_event_class, &pdev->dev, + opal_event_dev,NULL ,"opal_event" ); +if (IS_ERR (dev)) { + ret =PTR_ERR (dev); +dev_err (&pdev->dev,"device_create failed with %d\n" , ret);
arch/powerpc/platforms/powernv/opal-platform-events.c line 635
Quoted 10 lines
arch/powerpc/platforms/powernv/opal-platform-events.c new file +10
-0+ goto fail_class; + } + +cdev_init (&opal_event_cdev, &fops); + ret =cdev_add (&opal_event_cdev, opal_event_dev,OPAL_EVENT_MAX_DEVS ); +if (ret <0 ) { +dev_err (dev,"cdev_add failed\n" ); + ret = -EINVAL ; + goto fail_device; + }
You can probably avoid a lot of this by using a miscdev. Do we need a whole chardev class here?
arch/powerpc/platforms/powernv/opal-platform-events.c line 732
Quoted 12 lines
arch/powerpc/platforms/powernv/opal-platform-events.c new file +12
-0+static int __init opal_platform_event_init (void ) +{ + opal_event_probe_finished = false; +return platform_driver_register (&opal_event_driver); +} + +static void __exit opal_platform_event_exit(void) +{ +platform_driver_unregister (&opal_event_driver); +} +module_init (opal_platform_event_init); +module_exit (opal_platform_event_exit);
You can replace all of this with:
module_platform_driver(opal_event_driver);
And your probe function should only be called once. Are you sure you need that opal_event_probe_finished check?
Regards,
Jeremy