1 // SPDX-License-Identifier: GPL-2.0
3 * Xilinx Event Management Driver
5 * Copyright (C) 2021 Xilinx, Inc.
10 #include <linux/cpuhotplug.h>
11 #include <linux/firmware/xlnx-event-manager.h>
12 #include <linux/firmware/xlnx-zynqmp.h>
13 #include <linux/hashtable.h>
14 #include <linux/interrupt.h>
15 #include <linux/irq.h>
16 #include <linux/irqdomain.h>
17 #include <linux/module.h>
18 #include <linux/of_irq.h>
19 #include <linux/platform_device.h>
20 #include <linux/slab.h>
22 static DEFINE_PER_CPU_READ_MOSTLY(int, cpu_number1);
25 static int event_manager_availability = -EACCES;
27 /* SGI number used for Event management driver */
28 #define XLNX_EVENT_SGI_NUM (15)
30 /* Max number of driver can register for same event */
31 #define MAX_DRIVER_PER_EVENT (10U)
33 /* Max HashMap Order for PM API feature check (1<<7 = 128) */
34 #define REGISTERED_DRIVER_MAX_ORDER (7)
36 #define MAX_BITS (32U) /* Number of bits available for error mask */
38 #define FIRMWARE_VERSION_MASK (0xFFFFU)
39 #define REGISTER_NOTIFIER_FIRMWARE_VERSION (2U)
41 static DEFINE_HASHTABLE(reg_driver_map, REGISTERED_DRIVER_MAX_ORDER);
42 static int sgi_num = XLNX_EVENT_SGI_NUM;
44 static bool is_need_to_unregister;
47 * struct agent_cb - Registered callback function and private data.
48 * @agent_data: Data passed back to handler function.
49 * @eve_cb: Function pointer to store the callback function.
50 * @list: member to create list.
54 event_cb_func_t eve_cb;
55 struct list_head list;
59 * struct registered_event_data - Registered Event Data.
60 * @key: key is the combine id(Node-Id | Event-Id) of type u64
61 * where upper u32 for Node-Id and lower u32 for Event-Id,
62 * And this used as key to index into hashmap.
63 * @cb_type: Type of Api callback, like PM_NOTIFY_CB, etc.
64 * @wake: If this flag set, firmware will wake up processor if is
65 * in sleep or power down state.
66 * @cb_list_head: Head of call back data list which contain the information
67 * about registered handler and private data.
68 * @hentry: hlist_node that hooks this entry into hashtable.
70 struct registered_event_data {
72 enum pm_api_cb_id cb_type;
74 struct list_head cb_list_head;
75 struct hlist_node hentry;
78 static bool xlnx_is_error_event(const u32 node_id)
80 if (node_id == EVENT_ERROR_PMC_ERR1 ||
81 node_id == EVENT_ERROR_PMC_ERR2 ||
82 node_id == EVENT_ERROR_PSM_ERR1 ||
83 node_id == EVENT_ERROR_PSM_ERR2)
89 static int xlnx_add_cb_for_notify_event(const u32 node_id, const u32 event, const bool wake,
90 event_cb_func_t cb_fun, void *data)
93 bool present_in_hash = false;
94 struct registered_event_data *eve_data;
95 struct agent_cb *cb_data;
96 struct agent_cb *cb_pos;
97 struct agent_cb *cb_next;
99 key = ((u64)node_id << 32U) | (u64)event;
100 /* Check for existing entry in hash table for given key id */
101 hash_for_each_possible(reg_driver_map, eve_data, hentry, key) {
102 if (eve_data->key == key) {
103 present_in_hash = true;
108 if (!present_in_hash) {
109 /* Add new entry if not present in HASH table */
110 eve_data = kmalloc(sizeof(*eve_data), GFP_KERNEL);
114 eve_data->cb_type = PM_NOTIFY_CB;
115 eve_data->wake = wake;
116 INIT_LIST_HEAD(&eve_data->cb_list_head);
118 cb_data = kmalloc(sizeof(*cb_data), GFP_KERNEL);
123 cb_data->eve_cb = cb_fun;
124 cb_data->agent_data = data;
126 /* Add into callback list */
127 list_add(&cb_data->list, &eve_data->cb_list_head);
129 /* Add into HASH table */
130 hash_add(reg_driver_map, &eve_data->hentry, key);
132 /* Search for callback function and private data in list */
133 list_for_each_entry_safe(cb_pos, cb_next, &eve_data->cb_list_head, list) {
134 if (cb_pos->eve_cb == cb_fun &&
135 cb_pos->agent_data == data) {
140 /* Add multiple handler and private data in list */
141 cb_data = kmalloc(sizeof(*cb_data), GFP_KERNEL);
144 cb_data->eve_cb = cb_fun;
145 cb_data->agent_data = data;
147 list_add(&cb_data->list, &eve_data->cb_list_head);
153 static int xlnx_add_cb_for_suspend(event_cb_func_t cb_fun, void *data)
155 struct registered_event_data *eve_data;
156 struct agent_cb *cb_data;
158 /* Check for existing entry in hash table for given cb_type */
159 hash_for_each_possible(reg_driver_map, eve_data, hentry, PM_INIT_SUSPEND_CB) {
160 if (eve_data->cb_type == PM_INIT_SUSPEND_CB) {
161 pr_err("Found as already registered\n");
166 /* Add new entry if not present */
167 eve_data = kmalloc(sizeof(*eve_data), GFP_KERNEL);
172 eve_data->cb_type = PM_INIT_SUSPEND_CB;
173 INIT_LIST_HEAD(&eve_data->cb_list_head);
175 cb_data = kmalloc(sizeof(*cb_data), GFP_KERNEL);
178 cb_data->eve_cb = cb_fun;
179 cb_data->agent_data = data;
181 /* Add into callback list */
182 list_add(&cb_data->list, &eve_data->cb_list_head);
184 hash_add(reg_driver_map, &eve_data->hentry, PM_INIT_SUSPEND_CB);
189 static int xlnx_remove_cb_for_suspend(event_cb_func_t cb_fun)
191 bool is_callback_found = false;
192 struct registered_event_data *eve_data;
193 struct agent_cb *cb_pos;
194 struct agent_cb *cb_next;
195 struct hlist_node *tmp;
197 is_need_to_unregister = false;
199 /* Check for existing entry in hash table for given cb_type */
200 hash_for_each_possible_safe(reg_driver_map, eve_data, tmp, hentry, PM_INIT_SUSPEND_CB) {
201 if (eve_data->cb_type == PM_INIT_SUSPEND_CB) {
202 /* Delete the list of callback */
203 list_for_each_entry_safe(cb_pos, cb_next, &eve_data->cb_list_head, list) {
204 if (cb_pos->eve_cb == cb_fun) {
205 is_callback_found = true;
206 list_del_init(&cb_pos->list);
210 /* remove an object from a hashtable */
211 hash_del(&eve_data->hentry);
213 is_need_to_unregister = true;
216 if (!is_callback_found) {
217 pr_warn("Didn't find any registered callback for suspend event\n");
224 static int xlnx_remove_cb_for_notify_event(const u32 node_id, const u32 event,
225 event_cb_func_t cb_fun, void *data)
227 bool is_callback_found = false;
228 struct registered_event_data *eve_data;
229 u64 key = ((u64)node_id << 32U) | (u64)event;
230 struct agent_cb *cb_pos;
231 struct agent_cb *cb_next;
232 struct hlist_node *tmp;
234 is_need_to_unregister = false;
236 /* Check for existing entry in hash table for given key id */
237 hash_for_each_possible_safe(reg_driver_map, eve_data, tmp, hentry, key) {
238 if (eve_data->key == key) {
239 /* Delete the list of callback */
240 list_for_each_entry_safe(cb_pos, cb_next, &eve_data->cb_list_head, list) {
241 if (cb_pos->eve_cb == cb_fun &&
242 cb_pos->agent_data == data) {
243 is_callback_found = true;
244 list_del_init(&cb_pos->list);
249 /* Remove HASH table if callback list is empty */
250 if (list_empty(&eve_data->cb_list_head)) {
251 /* remove an object from a HASH table */
252 hash_del(&eve_data->hentry);
254 is_need_to_unregister = true;
258 if (!is_callback_found) {
259 pr_warn("Didn't find any registered callback for 0x%x 0x%x\n",
268 * xlnx_register_event() - Register for the event.
269 * @cb_type: Type of callback from pm_api_cb_id,
270 * PM_NOTIFY_CB - for Error Events,
271 * PM_INIT_SUSPEND_CB - for suspend callback.
272 * @node_id: Node-Id related to event.
273 * @event: Event Mask for the Error Event.
274 * @wake: Flag specifying whether the subsystem should be woken upon
275 * event notification.
276 * @cb_fun: Function pointer to store the callback function.
277 * @data: Pointer for the driver instance.
279 * Return: Returns 0 on successful registration else error code.
281 int xlnx_register_event(const enum pm_api_cb_id cb_type, const u32 node_id, const u32 event,
282 const bool wake, event_cb_func_t cb_fun, void *data)
288 if (event_manager_availability)
289 return event_manager_availability;
291 if (cb_type != PM_NOTIFY_CB && cb_type != PM_INIT_SUSPEND_CB) {
292 pr_err("%s() Unsupported Callback 0x%x\n", __func__, cb_type);
299 if (cb_type == PM_INIT_SUSPEND_CB) {
300 ret = xlnx_add_cb_for_suspend(cb_fun, data);
302 if (!xlnx_is_error_event(node_id)) {
303 /* Add entry for Node-Id/Event in hash table */
304 ret = xlnx_add_cb_for_notify_event(node_id, event, wake, cb_fun, data);
306 /* Add into Hash table */
307 for (pos = 0; pos < MAX_BITS; pos++) {
308 eve = event & (1 << pos);
312 /* Add entry for Node-Id/Eve in hash table */
313 ret = xlnx_add_cb_for_notify_event(node_id, eve, wake, cb_fun,
315 /* Break the loop if got error */
320 /* Skip the Event for which got the error */
322 /* Remove registered(during this call) event from hash table */
323 for ( ; pos >= 0; pos--) {
324 eve = event & (1 << pos);
327 xlnx_remove_cb_for_notify_event(node_id, eve, cb_fun, data);
333 pr_err("%s() failed for 0x%x and 0x%x: %d\r\n", __func__, node_id,
338 /* Register for Node-Id/Event combination in firmware */
339 ret = zynqmp_pm_register_notifier(node_id, event, wake, true);
341 pr_err("%s() failed for 0x%x and 0x%x: %d\r\n", __func__, node_id,
343 /* Remove already registered event from hash table */
344 if (xlnx_is_error_event(node_id)) {
345 for (pos = 0; pos < MAX_BITS; pos++) {
346 eve = event & (1 << pos);
349 xlnx_remove_cb_for_notify_event(node_id, eve, cb_fun, data);
352 xlnx_remove_cb_for_notify_event(node_id, event, cb_fun, data);
360 EXPORT_SYMBOL_GPL(xlnx_register_event);
363 * xlnx_unregister_event() - Unregister for the event.
364 * @cb_type: Type of callback from pm_api_cb_id,
365 * PM_NOTIFY_CB - for Error Events,
366 * PM_INIT_SUSPEND_CB - for suspend callback.
367 * @node_id: Node-Id related to event.
368 * @event: Event Mask for the Error Event.
369 * @cb_fun: Function pointer of callback function.
370 * @data: Pointer of agent's private data.
372 * Return: Returns 0 on successful unregistration else error code.
374 int xlnx_unregister_event(const enum pm_api_cb_id cb_type, const u32 node_id, const u32 event,
375 event_cb_func_t cb_fun, void *data)
380 is_need_to_unregister = false;
382 if (event_manager_availability)
383 return event_manager_availability;
385 if (cb_type != PM_NOTIFY_CB && cb_type != PM_INIT_SUSPEND_CB) {
386 pr_err("%s() Unsupported Callback 0x%x\n", __func__, cb_type);
393 if (cb_type == PM_INIT_SUSPEND_CB) {
394 ret = xlnx_remove_cb_for_suspend(cb_fun);
396 /* Remove Node-Id/Event from hash table */
397 if (!xlnx_is_error_event(node_id)) {
398 xlnx_remove_cb_for_notify_event(node_id, event, cb_fun, data);
400 for (pos = 0; pos < MAX_BITS; pos++) {
401 eve = event & (1 << pos);
405 xlnx_remove_cb_for_notify_event(node_id, eve, cb_fun, data);
409 /* Un-register if list is empty */
410 if (is_need_to_unregister) {
411 /* Un-register for Node-Id/Event combination */
412 ret = zynqmp_pm_register_notifier(node_id, event, false, false);
414 pr_err("%s() failed for 0x%x and 0x%x: %d\n",
415 __func__, node_id, event, ret);
423 EXPORT_SYMBOL_GPL(xlnx_unregister_event);
425 static void xlnx_call_suspend_cb_handler(const u32 *payload)
427 bool is_callback_found = false;
428 struct registered_event_data *eve_data;
429 u32 cb_type = payload[0];
430 struct agent_cb *cb_pos;
431 struct agent_cb *cb_next;
433 /* Check for existing entry in hash table for given cb_type */
434 hash_for_each_possible(reg_driver_map, eve_data, hentry, cb_type) {
435 if (eve_data->cb_type == cb_type) {
436 list_for_each_entry_safe(cb_pos, cb_next, &eve_data->cb_list_head, list) {
437 cb_pos->eve_cb(&payload[0], cb_pos->agent_data);
438 is_callback_found = true;
442 if (!is_callback_found)
443 pr_warn("Didn't find any registered callback for suspend event\n");
446 static void xlnx_call_notify_cb_handler(const u32 *payload)
448 bool is_callback_found = false;
449 struct registered_event_data *eve_data;
450 u64 key = ((u64)payload[1] << 32U) | (u64)payload[2];
452 struct agent_cb *cb_pos;
453 struct agent_cb *cb_next;
455 /* Check for existing entry in hash table for given key id */
456 hash_for_each_possible(reg_driver_map, eve_data, hentry, key) {
457 if (eve_data->key == key) {
458 list_for_each_entry_safe(cb_pos, cb_next, &eve_data->cb_list_head, list) {
459 cb_pos->eve_cb(&payload[0], cb_pos->agent_data);
460 is_callback_found = true;
463 /* re register with firmware to get future events */
464 ret = zynqmp_pm_register_notifier(payload[1], payload[2],
465 eve_data->wake, true);
467 pr_err("%s() failed for 0x%x and 0x%x: %d\r\n", __func__,
468 payload[1], payload[2], ret);
469 list_for_each_entry_safe(cb_pos, cb_next, &eve_data->cb_list_head,
471 /* Remove already registered event from hash table */
472 xlnx_remove_cb_for_notify_event(payload[1], payload[2],
479 if (!is_callback_found)
480 pr_warn("Didn't find any registered callback for 0x%x 0x%x\n",
481 payload[1], payload[2]);
484 static void xlnx_get_event_callback_data(u32 *buf)
486 zynqmp_pm_invoke_fn(GET_CALLBACK_DATA, 0, 0, 0, 0, buf);
489 static irqreturn_t xlnx_event_handler(int irq, void *dev_id)
491 u32 cb_type, node_id, event, pos;
492 u32 payload[CB_MAX_PAYLOAD_SIZE] = {0};
493 u32 event_data[CB_MAX_PAYLOAD_SIZE] = {0};
496 xlnx_get_event_callback_data(payload);
498 /* First element is callback type, others are callback arguments */
499 cb_type = payload[0];
501 if (cb_type == PM_NOTIFY_CB) {
502 node_id = payload[1];
504 if (!xlnx_is_error_event(node_id)) {
505 xlnx_call_notify_cb_handler(payload);
508 * Each call back function expecting payload as an input arguments.
509 * We can get multiple error events as in one call back through error
510 * mask. So payload[2] may can contain multiple error events.
511 * In reg_driver_map database we store data in the combination of single
512 * node_id-error combination.
513 * So coping the payload message into event_data and update the
514 * event_data[2] with Error Mask for single error event and use
515 * event_data as input argument for registered call back function.
518 memcpy(event_data, payload, (4 * CB_MAX_PAYLOAD_SIZE));
519 /* Support Multiple Error Event */
520 for (pos = 0; pos < MAX_BITS; pos++) {
521 if ((0 == (event & (1 << pos))))
523 event_data[2] = (event & (1 << pos));
524 xlnx_call_notify_cb_handler(event_data);
527 } else if (cb_type == PM_INIT_SUSPEND_CB) {
528 xlnx_call_suspend_cb_handler(payload);
530 pr_err("%s() Unsupported Callback %d\n", __func__, cb_type);
536 static int xlnx_event_cpuhp_start(unsigned int cpu)
538 enable_percpu_irq(virq_sgi, IRQ_TYPE_NONE);
543 static int xlnx_event_cpuhp_down(unsigned int cpu)
545 disable_percpu_irq(virq_sgi);
550 static void xlnx_disable_percpu_irq(void *data)
552 disable_percpu_irq(virq_sgi);
555 static int xlnx_event_init_sgi(struct platform_device *pdev)
558 int cpu = smp_processor_id();
560 * IRQ related structures are used for the following:
561 * for each SGI interrupt ensure its mapped by GIC IRQ domain
562 * and that each corresponding linux IRQ for the HW IRQ has
563 * a handler for when receiving an interrupt from the remote
566 struct irq_domain *domain;
567 struct irq_fwspec sgi_fwspec;
568 struct device_node *interrupt_parent = NULL;
569 struct device *parent = pdev->dev.parent;
571 /* Find GIC controller to map SGIs. */
572 interrupt_parent = of_irq_find_parent(parent->of_node);
573 if (!interrupt_parent) {
574 dev_err(&pdev->dev, "Failed to find property for Interrupt parent\n");
578 /* Each SGI needs to be associated with GIC's IRQ domain. */
579 domain = irq_find_host(interrupt_parent);
580 of_node_put(interrupt_parent);
582 /* Each mapping needs GIC domain when finding IRQ mapping. */
583 sgi_fwspec.fwnode = domain->fwnode;
586 * When irq domain looks at mapping each arg is as follows:
587 * 3 args for: interrupt type (SGI), interrupt # (set later), type
589 sgi_fwspec.param_count = 1;
591 /* Set SGI's hwirq */
592 sgi_fwspec.param[0] = sgi_num;
593 virq_sgi = irq_create_fwspec_mapping(&sgi_fwspec);
595 per_cpu(cpu_number1, cpu) = cpu;
596 ret = request_percpu_irq(virq_sgi, xlnx_event_handler, "xlnx_event_mgmt",
600 irq_dispose_mapping(virq_sgi);
604 irq_to_desc(virq_sgi);
605 irq_set_status_flags(virq_sgi, IRQ_PER_CPU);
610 static void xlnx_event_cleanup_sgi(struct platform_device *pdev)
612 int cpu = smp_processor_id();
614 per_cpu(cpu_number1, cpu) = cpu;
616 cpuhp_remove_state(CPUHP_AP_ONLINE_DYN);
618 on_each_cpu(xlnx_disable_percpu_irq, NULL, 1);
620 irq_clear_status_flags(virq_sgi, IRQ_PER_CPU);
621 free_percpu_irq(virq_sgi, &cpu_number1);
622 irq_dispose_mapping(virq_sgi);
625 static int xlnx_event_manager_probe(struct platform_device *pdev)
629 ret = zynqmp_pm_feature(PM_REGISTER_NOTIFIER);
631 dev_err(&pdev->dev, "Feature check failed with %d\n", ret);
635 if ((ret & FIRMWARE_VERSION_MASK) <
636 REGISTER_NOTIFIER_FIRMWARE_VERSION) {
637 dev_err(&pdev->dev, "Register notifier version error. Expected Firmware: v%d - Found: v%d\n",
638 REGISTER_NOTIFIER_FIRMWARE_VERSION,
639 ret & FIRMWARE_VERSION_MASK);
643 /* Initialize the SGI */
644 ret = xlnx_event_init_sgi(pdev);
646 dev_err(&pdev->dev, "SGI Init has been failed with %d\n", ret);
650 /* Setup function for the CPU hot-plug cases */
651 cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "soc/event:starting",
652 xlnx_event_cpuhp_start, xlnx_event_cpuhp_down);
654 ret = zynqmp_pm_register_sgi(sgi_num, 0);
656 dev_err(&pdev->dev, "SGI %d Registration over TF-A failed with %d\n", sgi_num, ret);
657 xlnx_event_cleanup_sgi(pdev);
661 event_manager_availability = 0;
663 dev_info(&pdev->dev, "SGI %d Registered over TF-A\n", sgi_num);
664 dev_info(&pdev->dev, "Xilinx Event Management driver probed\n");
669 static int xlnx_event_manager_remove(struct platform_device *pdev)
672 struct registered_event_data *eve_data;
673 struct hlist_node *tmp;
675 struct agent_cb *cb_pos;
676 struct agent_cb *cb_next;
678 hash_for_each_safe(reg_driver_map, i, tmp, eve_data, hentry) {
679 list_for_each_entry_safe(cb_pos, cb_next, &eve_data->cb_list_head, list) {
680 list_del_init(&cb_pos->list);
683 hash_del(&eve_data->hentry);
687 ret = zynqmp_pm_register_sgi(0, 1);
689 dev_err(&pdev->dev, "SGI unregistration over TF-A failed with %d\n", ret);
691 xlnx_event_cleanup_sgi(pdev);
693 event_manager_availability = -EACCES;
698 static struct platform_driver xlnx_event_manager_driver = {
699 .probe = xlnx_event_manager_probe,
700 .remove = xlnx_event_manager_remove,
702 .name = "xlnx_event_manager",
705 module_param(sgi_num, uint, 0);
706 module_platform_driver(xlnx_event_manager_driver);