accel/ivpu: Move recovery work to system_unbound_wq
[drm/drm-misc.git] / drivers / acpi / arm64 / apmt.c
blobbb010f6164e528acd802f65afd80d65a57f5844f
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * ARM APMT table support.
4 * Design document number: ARM DEN0117.
6 * Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES.
8 */
10 #define pr_fmt(fmt) "ACPI: APMT: " fmt
12 #include <linux/acpi.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/platform_device.h>
16 #include "init.h"
18 #define DEV_NAME "arm-cs-arch-pmu"
20 /* There can be up to 3 resources: page 0 and 1 address, and interrupt. */
21 #define DEV_MAX_RESOURCE_COUNT 3
23 /* Root pointer to the mapped APMT table */
24 static struct acpi_table_header *apmt_table;
26 static int __init apmt_init_resources(struct resource *res,
27 struct acpi_apmt_node *node)
29 int irq, trigger;
30 int num_res = 0;
32 res[num_res].start = node->base_address0;
33 res[num_res].end = node->base_address0 + SZ_4K - 1;
34 res[num_res].flags = IORESOURCE_MEM;
36 num_res++;
38 if (node->flags & ACPI_APMT_FLAGS_DUAL_PAGE) {
39 res[num_res].start = node->base_address1;
40 res[num_res].end = node->base_address1 + SZ_4K - 1;
41 res[num_res].flags = IORESOURCE_MEM;
43 num_res++;
46 if (node->ovflw_irq != 0) {
47 trigger = (node->ovflw_irq_flags & ACPI_APMT_OVFLW_IRQ_FLAGS_MODE);
48 trigger = (trigger == ACPI_APMT_OVFLW_IRQ_FLAGS_MODE_LEVEL) ?
49 ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
50 irq = acpi_register_gsi(NULL, node->ovflw_irq, trigger,
51 ACPI_ACTIVE_HIGH);
53 if (irq <= 0) {
54 pr_warn("APMT could not register gsi hwirq %d\n", irq);
55 return num_res;
58 res[num_res].start = irq;
59 res[num_res].end = irq;
60 res[num_res].flags = IORESOURCE_IRQ;
62 num_res++;
65 return num_res;
68 /**
69 * apmt_add_platform_device() - Allocate a platform device for APMT node
70 * @node: Pointer to device ACPI APMT node
71 * @fwnode: fwnode associated with the APMT node
73 * Returns: 0 on success, <0 failure
75 static int __init apmt_add_platform_device(struct acpi_apmt_node *node,
76 struct fwnode_handle *fwnode)
78 struct platform_device *pdev;
79 int ret, count;
80 struct resource res[DEV_MAX_RESOURCE_COUNT];
82 pdev = platform_device_alloc(DEV_NAME, PLATFORM_DEVID_AUTO);
83 if (!pdev)
84 return -ENOMEM;
86 memset(res, 0, sizeof(res));
88 count = apmt_init_resources(res, node);
90 ret = platform_device_add_resources(pdev, res, count);
91 if (ret)
92 goto dev_put;
95 * Add a copy of APMT node pointer to platform_data to be used to
96 * retrieve APMT data information.
98 ret = platform_device_add_data(pdev, &node, sizeof(node));
99 if (ret)
100 goto dev_put;
102 pdev->dev.fwnode = fwnode;
104 ret = platform_device_add(pdev);
106 if (ret)
107 goto dev_put;
109 return 0;
111 dev_put:
112 platform_device_put(pdev);
114 return ret;
117 static int __init apmt_init_platform_devices(void)
119 struct acpi_apmt_node *apmt_node;
120 struct acpi_table_apmt *apmt;
121 struct fwnode_handle *fwnode;
122 u64 offset, end;
123 int ret;
126 * apmt_table and apmt both point to the start of APMT table, but
127 * have different struct types
129 apmt = (struct acpi_table_apmt *)apmt_table;
130 offset = sizeof(*apmt);
131 end = apmt->header.length;
133 while (offset < end) {
134 apmt_node = ACPI_ADD_PTR(struct acpi_apmt_node, apmt,
135 offset);
137 fwnode = acpi_alloc_fwnode_static();
138 if (!fwnode)
139 return -ENOMEM;
141 ret = apmt_add_platform_device(apmt_node, fwnode);
142 if (ret) {
143 acpi_free_fwnode_static(fwnode);
144 return ret;
147 offset += apmt_node->length;
150 return 0;
153 void __init acpi_apmt_init(void)
155 acpi_status status;
156 int ret;
159 * APMT table nodes will be used at runtime after the apmt init,
160 * so we don't need to call acpi_put_table() to release
161 * the APMT table mapping.
163 status = acpi_get_table(ACPI_SIG_APMT, 0, &apmt_table);
165 if (ACPI_FAILURE(status)) {
166 if (status != AE_NOT_FOUND) {
167 const char *msg = acpi_format_exception(status);
169 pr_err("Failed to get APMT table, %s\n", msg);
172 return;
175 ret = apmt_init_platform_devices();
176 if (ret) {
177 pr_err("Failed to initialize APMT platform devices, ret: %d\n", ret);
178 acpi_put_table(apmt_table);