1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * AMD HSMP Platform Driver
4 * Copyright (c) 2024, AMD.
5 * All Rights Reserved.
6 *
7 * This file provides an ACPI based driver implementation for HSMP interface.
8 */
9
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12 #include <asm/amd/hsmp.h>
13
14 #include <linux/acpi.h>
15 #include <linux/array_size.h>
16 #include <linux/bits.h>
17 #include <linux/bitfield.h>
18 #include <linux/cleanup.h>
19 #include <linux/device.h>
20 #include <linux/dev_printk.h>
21 #include <linux/ioport.h>
22 #include <linux/kref.h>
23 #include <linux/kstrtox.h>
24 #include <linux/lockdep.h>
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/rwsem.h>
28 #include <linux/slab.h>
29 #include <linux/string.h>
30 #include <linux/sysfs.h>
31 #include <linux/topology.h>
32 #include <linux/uuid.h>
33
34 #include <uapi/asm-generic/errno-base.h>
35
36 #include "hsmp.h"
37
38 #define DRIVER_NAME "hsmp_acpi"
39
40 /* These are the strings specified in ACPI table */
41 #define MSG_IDOFF_STR "MsgIdOffset"
42 #define MSG_ARGOFF_STR "MsgArgOffset"
43 #define MSG_RESPOFF_STR "MsgRspOffset"
44
45 static struct hsmp_plat_device *hsmp_pdev;
46
47 /*
48 * Tracks the ACPI socket platform devices that share the socket array and the
49 * /dev/hsmp misc device. The first probe initializes it, each further probe
50 * takes a reference and every remove (or probe failure) drops one; the last
51 * put frees the shared state via hsmp_acpi_sock_release(). All get/put run
52 * under hsmp_sock_rwsem held for write, so the counting is already serialized
53 * and the atomic in kref is not strictly needed; kref is used for the clearer
54 * get/put interface and its release callback.
55 */
56 static struct kref hsmp_acpi_sock_kref;
57
58 struct hsmp_sys_attr {
59 struct device_attribute dattr;
60 u32 msg_id;
61 };
62
amd_hsmp_acpi_rdwr(struct hsmp_socket * sock,u32 offset,u32 * value,bool write)63 static int amd_hsmp_acpi_rdwr(struct hsmp_socket *sock, u32 offset,
64 u32 *value, bool write)
65 {
66 if (write)
67 iowrite32(*value, sock->virt_base_addr + offset);
68 else
69 *value = ioread32(sock->virt_base_addr + offset);
70
71 return 0;
72 }
73
74 /* This is the UUID used for HSMP */
75 static const guid_t acpi_hsmp_uuid = GUID_INIT(0xb74d619d, 0x5707, 0x48bd,
76 0xa6, 0x9f, 0x4e, 0xa2,
77 0x87, 0x1f, 0xc2, 0xf6);
78
is_acpi_hsmp_uuid(union acpi_object * obj)79 static inline bool is_acpi_hsmp_uuid(union acpi_object *obj)
80 {
81 if (obj->type == ACPI_TYPE_BUFFER && obj->buffer.length == UUID_SIZE)
82 return guid_equal((guid_t *)obj->buffer.pointer, &acpi_hsmp_uuid);
83
84 return false;
85 }
86
hsmp_get_uid(struct device * dev,u16 * sock_ind)87 static inline int hsmp_get_uid(struct device *dev, u16 *sock_ind)
88 {
89 char *uid;
90
91 /*
92 * UID (ID00, ID01..IDXX) is used for differentiating sockets,
93 * read it and strip the "ID" part of it and convert the remaining
94 * bytes to integer.
95 */
96 uid = acpi_device_uid(ACPI_COMPANION(dev));
97 if (!uid || strlen(uid) < 3)
98 return -EINVAL;
99
100 return kstrtou16(uid + 2, 10, sock_ind);
101 }
102
hsmp_resource(struct acpi_resource * res,void * data)103 static acpi_status hsmp_resource(struct acpi_resource *res, void *data)
104 {
105 struct hsmp_socket *sock = data;
106 struct resource r;
107
108 switch (res->type) {
109 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
110 if (!acpi_dev_resource_memory(res, &r))
111 return AE_ERROR;
112 if (!r.start || r.end < r.start || !(r.flags & IORESOURCE_MEM_WRITEABLE))
113 return AE_ERROR;
114 sock->mbinfo.base_addr = r.start;
115 sock->mbinfo.size = resource_size(&r);
116 break;
117 case ACPI_RESOURCE_TYPE_END_TAG:
118 break;
119 default:
120 return AE_ERROR;
121 }
122
123 return AE_OK;
124 }
125
hsmp_read_acpi_dsd(struct device * dev,struct hsmp_socket * sock)126 static int hsmp_read_acpi_dsd(struct device *dev, struct hsmp_socket *sock)
127 {
128 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
129 union acpi_object *guid, *mailbox_package;
130 union acpi_object *dsd;
131 acpi_status status;
132 int ret = 0;
133 int j;
134
135 status = acpi_evaluate_object_typed(ACPI_HANDLE(dev), "_DSD", NULL,
136 &buf, ACPI_TYPE_PACKAGE);
137 if (ACPI_FAILURE(status)) {
138 dev_err(dev, "Failed to read mailbox reg offsets from DSD table, err: %s\n",
139 acpi_format_exception(status));
140 return -ENODEV;
141 }
142
143 dsd = buf.pointer;
144
145 /* HSMP _DSD property should contain 2 objects.
146 * 1. guid which is an acpi object of type ACPI_TYPE_BUFFER
147 * 2. mailbox which is an acpi object of type ACPI_TYPE_PACKAGE
148 * This mailbox object contains 3 more acpi objects of type
149 * ACPI_TYPE_PACKAGE for holding msgid, msgresp, msgarg offsets
150 * these packages inturn contain 2 acpi objects of type
151 * ACPI_TYPE_STRING and ACPI_TYPE_INTEGER
152 */
153 if (!dsd || dsd->type != ACPI_TYPE_PACKAGE || dsd->package.count != 2) {
154 ret = -EINVAL;
155 goto free_buf;
156 }
157
158 guid = &dsd->package.elements[0];
159 mailbox_package = &dsd->package.elements[1];
160 if (!is_acpi_hsmp_uuid(guid) || mailbox_package->type != ACPI_TYPE_PACKAGE) {
161 dev_err(dev, "Invalid hsmp _DSD table data\n");
162 ret = -EINVAL;
163 goto free_buf;
164 }
165
166 for (j = 0; j < mailbox_package->package.count; j++) {
167 union acpi_object *msgobj, *msgstr, *msgint;
168
169 msgobj = &mailbox_package->package.elements[j];
170
171 /* package should have 1 string and 1 integer object */
172 if (msgobj->type != ACPI_TYPE_PACKAGE ||
173 msgobj->package.count < 2) {
174 ret = -EINVAL;
175 goto free_buf;
176 }
177
178 msgstr = &msgobj->package.elements[0];
179 msgint = &msgobj->package.elements[1];
180
181 if (msgstr->type != ACPI_TYPE_STRING ||
182 msgint->type != ACPI_TYPE_INTEGER) {
183 ret = -EINVAL;
184 goto free_buf;
185 }
186
187 if (!strncmp(msgstr->string.pointer, MSG_IDOFF_STR,
188 msgstr->string.length)) {
189 sock->mbinfo.msg_id_off = msgint->integer.value;
190 } else if (!strncmp(msgstr->string.pointer, MSG_RESPOFF_STR,
191 msgstr->string.length)) {
192 sock->mbinfo.msg_resp_off = msgint->integer.value;
193 } else if (!strncmp(msgstr->string.pointer, MSG_ARGOFF_STR,
194 msgstr->string.length)) {
195 sock->mbinfo.msg_arg_off = msgint->integer.value;
196 } else {
197 ret = -ENOENT;
198 goto free_buf;
199 }
200 }
201
202 if (!sock->mbinfo.msg_id_off || !sock->mbinfo.msg_resp_off ||
203 !sock->mbinfo.msg_arg_off)
204 ret = -EINVAL;
205
206 free_buf:
207 ACPI_FREE(buf.pointer);
208 return ret;
209 }
210
hsmp_read_acpi_crs(struct device * dev,struct hsmp_socket * sock)211 static int hsmp_read_acpi_crs(struct device *dev, struct hsmp_socket *sock)
212 {
213 acpi_status status;
214
215 status = acpi_walk_resources(ACPI_HANDLE(dev), METHOD_NAME__CRS,
216 hsmp_resource, sock);
217 if (ACPI_FAILURE(status)) {
218 dev_err(dev, "Failed to look up MP1 base address from CRS method, err: %s\n",
219 acpi_format_exception(status));
220 return -EINVAL;
221 }
222 if (!sock->mbinfo.base_addr || !sock->mbinfo.size)
223 return -EINVAL;
224
225 /* The mapped region should be un-cached */
226 sock->virt_base_addr = devm_ioremap_uc(dev, sock->mbinfo.base_addr,
227 sock->mbinfo.size);
228 if (!sock->virt_base_addr) {
229 dev_err(dev, "Failed to ioremap MP1 base address\n");
230 return -ENOMEM;
231 }
232
233 return 0;
234 }
235
236 /* Parse the ACPI table to read the data */
hsmp_parse_acpi_table(struct device * dev,u16 sock_ind)237 static int hsmp_parse_acpi_table(struct device *dev, u16 sock_ind)
238 {
239 struct hsmp_socket *sock = &hsmp_pdev->sock[sock_ind];
240 int ret;
241
242 sock->sock_ind = sock_ind;
243 sock->amd_hsmp_rdwr = amd_hsmp_acpi_rdwr;
244
245 sema_init(&sock->hsmp_sem, 1);
246
247 dev_set_drvdata(dev, sock);
248
249 /* Read MP1 base address from CRS method */
250 ret = hsmp_read_acpi_crs(dev, sock);
251 if (ret)
252 return ret;
253
254 /* Read mailbox offsets from DSD table */
255 ret = hsmp_read_acpi_dsd(dev, sock);
256 if (ret)
257 return ret;
258
259 /*
260 * Publish sock->dev last. hsmp_send_message() uses it (via
261 * smp_load_acquire()) as the readiness gate for the lock-free data
262 * plane, so it must become visible only after virt_base_addr, the
263 * mailbox offsets and the semaphore are fully initialized. On a
264 * multi-socket system socket 0 exposes /dev/hsmp before later sockets
265 * finish probing, so without this an ioctl aimed at a socket still in
266 * bring-up could pass the gate and dereference a NULL virt_base_addr.
267 */
268 smp_store_release(&sock->dev, dev);
269
270 return 0;
271 }
272
hsmp_metric_tbl_acpi_read(struct file * filp,struct kobject * kobj,const struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)273 static ssize_t hsmp_metric_tbl_acpi_read(struct file *filp, struct kobject *kobj,
274 const struct bin_attribute *bin_attr, char *buf,
275 loff_t off, size_t count)
276 {
277 struct device *dev = container_of(kobj, struct device, kobj);
278 struct hsmp_socket *sock = dev_get_drvdata(dev);
279
280 /*
281 * metrics_bin is a sysfs binary attribute and is capped at PAGE_SIZE.
282 * It can therefore only carry the protocol version 6 metric table
283 * (struct hsmp_metric_table). The larger tables defined from protocol
284 * version 7 onwards do not fit; userspace on those systems must read
285 * the snapshot through HSMP_IOCTL_GET_TELEMETRY_DATA on /dev/hsmp.
286 * Surface the unsupported case here as -EOPNOTSUPP rather than
287 * silently truncating the snapshot.
288 */
289 if (hsmp_pdev->proto_ver != HSMP_PROTO_VER6)
290 return -EOPNOTSUPP;
291
292 return hsmp_metric_tbl_read(sock, buf, count);
293 }
294
hsmp_is_sock_attr_visible(struct kobject * kobj,const struct bin_attribute * battr,int id)295 static umode_t hsmp_is_sock_attr_visible(struct kobject *kobj,
296 const struct bin_attribute *battr, int id)
297 {
298 /*
299 * Keep metrics_bin visible on protocol version 7 and later as well,
300 * so that userspace which expects the file to exist gets a clear
301 * -EOPNOTSUPP from the read handler instead of -ENOENT, and is
302 * pointed at HSMP_IOCTL_GET_TELEMETRY_DATA as the supported path.
303 */
304 if (hsmp_pdev->proto_ver >= HSMP_PROTO_VER6)
305 return battr->attr.mode;
306
307 return 0;
308 }
309
hsmp_is_sock_dev_attr_visible(struct kobject * kobj,struct attribute * attr,int id)310 static umode_t hsmp_is_sock_dev_attr_visible(struct kobject *kobj,
311 struct attribute *attr, int id)
312 {
313 return attr->mode;
314 }
315
316 #define to_hsmp_sys_attr(_attr) container_of(_attr, struct hsmp_sys_attr, dattr)
317
hsmp_msg_resp32_show(struct device * dev,struct device_attribute * attr,char * buf)318 static ssize_t hsmp_msg_resp32_show(struct device *dev, struct device_attribute *attr,
319 char *buf)
320 {
321 struct hsmp_sys_attr *hattr = to_hsmp_sys_attr(attr);
322 struct hsmp_socket *sock = dev_get_drvdata(dev);
323 u32 data;
324 int ret;
325
326 ret = hsmp_msg_get_nargs(sock->sock_ind, hattr->msg_id, &data, 1);
327 if (ret)
328 return ret;
329
330 return sysfs_emit(buf, "%u\n", data);
331 }
332
333 #define DDR_MAX_BW_MASK GENMASK(31, 20)
334 #define DDR_UTIL_BW_MASK GENMASK(19, 8)
335 #define DDR_UTIL_BW_PERC_MASK GENMASK(7, 0)
336 #define FW_VER_MAJOR_MASK GENMASK(23, 16)
337 #define FW_VER_MINOR_MASK GENMASK(15, 8)
338 #define FW_VER_DEBUG_MASK GENMASK(7, 0)
339 #define FMAX_MASK GENMASK(31, 16)
340 #define FMIN_MASK GENMASK(15, 0)
341 #define FREQ_LIMIT_MASK GENMASK(31, 16)
342 #define FREQ_SRC_IND_MASK GENMASK(15, 0)
343
hsmp_ddr_max_bw_show(struct device * dev,struct device_attribute * attr,char * buf)344 static ssize_t hsmp_ddr_max_bw_show(struct device *dev, struct device_attribute *attr,
345 char *buf)
346 {
347 struct hsmp_sys_attr *hattr = to_hsmp_sys_attr(attr);
348 struct hsmp_socket *sock = dev_get_drvdata(dev);
349 u32 data;
350 int ret;
351
352 ret = hsmp_msg_get_nargs(sock->sock_ind, hattr->msg_id, &data, 1);
353 if (ret)
354 return ret;
355
356 return sysfs_emit(buf, "%lu\n", FIELD_GET(DDR_MAX_BW_MASK, data));
357 }
358
hsmp_ddr_util_bw_show(struct device * dev,struct device_attribute * attr,char * buf)359 static ssize_t hsmp_ddr_util_bw_show(struct device *dev, struct device_attribute *attr,
360 char *buf)
361 {
362 struct hsmp_sys_attr *hattr = to_hsmp_sys_attr(attr);
363 struct hsmp_socket *sock = dev_get_drvdata(dev);
364 u32 data;
365 int ret;
366
367 ret = hsmp_msg_get_nargs(sock->sock_ind, hattr->msg_id, &data, 1);
368 if (ret)
369 return ret;
370
371 return sysfs_emit(buf, "%lu\n", FIELD_GET(DDR_UTIL_BW_MASK, data));
372 }
373
hsmp_ddr_util_bw_perc_show(struct device * dev,struct device_attribute * attr,char * buf)374 static ssize_t hsmp_ddr_util_bw_perc_show(struct device *dev, struct device_attribute *attr,
375 char *buf)
376 {
377 struct hsmp_sys_attr *hattr = to_hsmp_sys_attr(attr);
378 struct hsmp_socket *sock = dev_get_drvdata(dev);
379 u32 data;
380 int ret;
381
382 ret = hsmp_msg_get_nargs(sock->sock_ind, hattr->msg_id, &data, 1);
383 if (ret)
384 return ret;
385
386 return sysfs_emit(buf, "%lu\n", FIELD_GET(DDR_UTIL_BW_PERC_MASK, data));
387 }
388
hsmp_msg_fw_ver_show(struct device * dev,struct device_attribute * attr,char * buf)389 static ssize_t hsmp_msg_fw_ver_show(struct device *dev, struct device_attribute *attr,
390 char *buf)
391 {
392 struct hsmp_sys_attr *hattr = to_hsmp_sys_attr(attr);
393 struct hsmp_socket *sock = dev_get_drvdata(dev);
394 u32 data;
395 int ret;
396
397 ret = hsmp_msg_get_nargs(sock->sock_ind, hattr->msg_id, &data, 1);
398 if (ret)
399 return ret;
400
401 return sysfs_emit(buf, "%lu.%lu.%lu\n",
402 FIELD_GET(FW_VER_MAJOR_MASK, data),
403 FIELD_GET(FW_VER_MINOR_MASK, data),
404 FIELD_GET(FW_VER_DEBUG_MASK, data));
405 }
406
hsmp_fclk_show(struct device * dev,struct device_attribute * attr,char * buf)407 static ssize_t hsmp_fclk_show(struct device *dev, struct device_attribute *attr,
408 char *buf)
409 {
410 struct hsmp_sys_attr *hattr = to_hsmp_sys_attr(attr);
411 struct hsmp_socket *sock = dev_get_drvdata(dev);
412 u32 data[2];
413 int ret;
414
415 ret = hsmp_msg_get_nargs(sock->sock_ind, hattr->msg_id, data, 2);
416 if (ret)
417 return ret;
418
419 return sysfs_emit(buf, "%u\n", data[0]);
420 }
421
hsmp_mclk_show(struct device * dev,struct device_attribute * attr,char * buf)422 static ssize_t hsmp_mclk_show(struct device *dev, struct device_attribute *attr,
423 char *buf)
424 {
425 struct hsmp_sys_attr *hattr = to_hsmp_sys_attr(attr);
426 struct hsmp_socket *sock = dev_get_drvdata(dev);
427 u32 data[2];
428 int ret;
429
430 ret = hsmp_msg_get_nargs(sock->sock_ind, hattr->msg_id, data, 2);
431 if (ret)
432 return ret;
433
434 return sysfs_emit(buf, "%u\n", data[1]);
435 }
436
hsmp_clk_fmax_show(struct device * dev,struct device_attribute * attr,char * buf)437 static ssize_t hsmp_clk_fmax_show(struct device *dev, struct device_attribute *attr,
438 char *buf)
439 {
440 struct hsmp_sys_attr *hattr = to_hsmp_sys_attr(attr);
441 struct hsmp_socket *sock = dev_get_drvdata(dev);
442 u32 data;
443 int ret;
444
445 ret = hsmp_msg_get_nargs(sock->sock_ind, hattr->msg_id, &data, 1);
446 if (ret)
447 return ret;
448
449 return sysfs_emit(buf, "%lu\n", FIELD_GET(FMAX_MASK, data));
450 }
451
hsmp_clk_fmin_show(struct device * dev,struct device_attribute * attr,char * buf)452 static ssize_t hsmp_clk_fmin_show(struct device *dev, struct device_attribute *attr,
453 char *buf)
454 {
455 struct hsmp_sys_attr *hattr = to_hsmp_sys_attr(attr);
456 struct hsmp_socket *sock = dev_get_drvdata(dev);
457 u32 data;
458 int ret;
459
460 ret = hsmp_msg_get_nargs(sock->sock_ind, hattr->msg_id, &data, 1);
461 if (ret)
462 return ret;
463
464 return sysfs_emit(buf, "%lu\n", FIELD_GET(FMIN_MASK, data));
465 }
466
hsmp_freq_limit_show(struct device * dev,struct device_attribute * attr,char * buf)467 static ssize_t hsmp_freq_limit_show(struct device *dev, struct device_attribute *attr,
468 char *buf)
469 {
470 struct hsmp_sys_attr *hattr = to_hsmp_sys_attr(attr);
471 struct hsmp_socket *sock = dev_get_drvdata(dev);
472 u32 data;
473 int ret;
474
475 ret = hsmp_msg_get_nargs(sock->sock_ind, hattr->msg_id, &data, 1);
476 if (ret)
477 return ret;
478
479 return sysfs_emit(buf, "%lu\n", FIELD_GET(FREQ_LIMIT_MASK, data));
480 }
481
482 static const char * const freqlimit_srcnames[] = {
483 "cHTC-Active",
484 "PROCHOT",
485 "TDC limit",
486 "PPT Limit",
487 "OPN Max",
488 "Reliability Limit",
489 "APML Agent",
490 "HSMP Agent",
491 };
492
hsmp_freq_limit_source_show(struct device * dev,struct device_attribute * attr,char * buf)493 static ssize_t hsmp_freq_limit_source_show(struct device *dev, struct device_attribute *attr,
494 char *buf)
495 {
496 struct hsmp_sys_attr *hattr = to_hsmp_sys_attr(attr);
497 struct hsmp_socket *sock = dev_get_drvdata(dev);
498 unsigned int index;
499 int len = 0;
500 u16 src_ind;
501 u32 data;
502 int ret;
503
504 ret = hsmp_msg_get_nargs(sock->sock_ind, hattr->msg_id, &data, 1);
505 if (ret)
506 return ret;
507
508 src_ind = FIELD_GET(FREQ_SRC_IND_MASK, data);
509 for (index = 0; index < ARRAY_SIZE(freqlimit_srcnames); index++) {
510 if (!src_ind)
511 break;
512 if (src_ind & 1)
513 len += sysfs_emit_at(buf, len, "%s\n", freqlimit_srcnames[index]);
514 src_ind >>= 1;
515 }
516 return len;
517 }
518
519 /*
520 * Bring up one ACPI HSMP socket: parse its ACPI table, run the mailbox
521 * handshake and register its sysfs/hwmon interfaces.
522 *
523 * Called with hsmp_sock_rwsem held for write by hsmp_acpi_probe(), so the
524 * per-socket bring-up cannot race a concurrent probe or remove.
525 */
init_acpi(struct device * dev)526 static int init_acpi(struct device *dev)
527 {
528 u16 sock_ind;
529 int ret;
530
531 lockdep_assert_held_write(&hsmp_sock_rwsem);
532
533 ret = hsmp_get_uid(dev, &sock_ind);
534 if (ret)
535 return ret;
536 if (sock_ind >= hsmp_pdev->num_sockets)
537 return -EINVAL;
538
539 ret = hsmp_parse_acpi_table(dev, sock_ind);
540 if (ret) {
541 dev_err(dev, "Failed to parse ACPI table\n");
542 return ret;
543 }
544
545 /* Test the hsmp interface */
546 ret = hsmp_test(sock_ind, 0xDEADBEEF);
547 if (ret) {
548 dev_err(dev, "HSMP test message failed on Fam:%x model:%x\n",
549 boot_cpu_data.x86, boot_cpu_data.x86_model);
550 dev_err(dev, "Is HSMP disabled in BIOS ?\n");
551 return ret;
552 }
553
554 ret = hsmp_cache_proto_ver(sock_ind);
555 if (ret) {
556 dev_err(dev, "Failed to read HSMP protocol version\n");
557 return ret;
558 }
559
560 if (hsmp_pdev->proto_ver >= HSMP_PROTO_VER6) {
561 ret = hsmp_get_tbl_dram_base(sock_ind);
562 if (ret)
563 dev_info(dev, "Failed to init metric table\n");
564 }
565
566 ret = hsmp_create_sensor(dev, sock_ind);
567 if (ret)
568 dev_info(dev, "Failed to register HSMP sensors with hwmon\n");
569
570 dev_set_drvdata(dev, &hsmp_pdev->sock[sock_ind]);
571
572 return 0;
573 }
574
575 static const struct bin_attribute hsmp_metric_tbl_attr = {
576 .attr = { .name = HSMP_METRICS_TABLE_NAME, .mode = 0444},
577 .read = hsmp_metric_tbl_acpi_read,
578 .size = sizeof(struct hsmp_metric_table),
579 };
580
581 static const struct bin_attribute *hsmp_attr_list[] = {
582 &hsmp_metric_tbl_attr,
583 NULL
584 };
585
586 #define HSMP_DEV_ATTR(_name, _msg_id, _show, _mode) \
587 static struct hsmp_sys_attr hattr_##_name = { \
588 .dattr = __ATTR(_name, _mode, _show, NULL), \
589 .msg_id = _msg_id, \
590 }
591
592 HSMP_DEV_ATTR(c0_residency_input, HSMP_GET_C0_PERCENT, hsmp_msg_resp32_show, 0444);
593 HSMP_DEV_ATTR(prochot_status, HSMP_GET_PROC_HOT, hsmp_msg_resp32_show, 0444);
594 HSMP_DEV_ATTR(smu_fw_version, HSMP_GET_SMU_VER, hsmp_msg_fw_ver_show, 0444);
595 HSMP_DEV_ATTR(protocol_version, HSMP_GET_PROTO_VER, hsmp_msg_resp32_show, 0444);
596 HSMP_DEV_ATTR(cclk_freq_limit_input, HSMP_GET_CCLK_THROTTLE_LIMIT, hsmp_msg_resp32_show, 0444);
597 HSMP_DEV_ATTR(ddr_max_bw, HSMP_GET_DDR_BANDWIDTH, hsmp_ddr_max_bw_show, 0444);
598 HSMP_DEV_ATTR(ddr_utilised_bw_input, HSMP_GET_DDR_BANDWIDTH, hsmp_ddr_util_bw_show, 0444);
599 HSMP_DEV_ATTR(ddr_utilised_bw_perc_input, HSMP_GET_DDR_BANDWIDTH, hsmp_ddr_util_bw_perc_show, 0444);
600 HSMP_DEV_ATTR(fclk_input, HSMP_GET_FCLK_MCLK, hsmp_fclk_show, 0444);
601 HSMP_DEV_ATTR(mclk_input, HSMP_GET_FCLK_MCLK, hsmp_mclk_show, 0444);
602 HSMP_DEV_ATTR(clk_fmax, HSMP_GET_SOCKET_FMAX_FMIN, hsmp_clk_fmax_show, 0444);
603 HSMP_DEV_ATTR(clk_fmin, HSMP_GET_SOCKET_FMAX_FMIN, hsmp_clk_fmin_show, 0444);
604 HSMP_DEV_ATTR(pwr_current_active_freq_limit, HSMP_GET_SOCKET_FREQ_LIMIT,
605 hsmp_freq_limit_show, 0444);
606 HSMP_DEV_ATTR(pwr_current_active_freq_limit_source, HSMP_GET_SOCKET_FREQ_LIMIT,
607 hsmp_freq_limit_source_show, 0444);
608
609 static struct attribute *hsmp_dev_attr_list[] = {
610 &hattr_c0_residency_input.dattr.attr,
611 &hattr_prochot_status.dattr.attr,
612 &hattr_smu_fw_version.dattr.attr,
613 &hattr_protocol_version.dattr.attr,
614 &hattr_cclk_freq_limit_input.dattr.attr,
615 &hattr_ddr_max_bw.dattr.attr,
616 &hattr_ddr_utilised_bw_input.dattr.attr,
617 &hattr_ddr_utilised_bw_perc_input.dattr.attr,
618 &hattr_fclk_input.dattr.attr,
619 &hattr_mclk_input.dattr.attr,
620 &hattr_clk_fmax.dattr.attr,
621 &hattr_clk_fmin.dattr.attr,
622 &hattr_pwr_current_active_freq_limit.dattr.attr,
623 &hattr_pwr_current_active_freq_limit_source.dattr.attr,
624 NULL
625 };
626
627 static const struct attribute_group hsmp_attr_grp = {
628 .bin_attrs = hsmp_attr_list,
629 .attrs = hsmp_dev_attr_list,
630 .is_bin_visible = hsmp_is_sock_attr_visible,
631 .is_visible = hsmp_is_sock_dev_attr_visible,
632 };
633
634 static const struct attribute_group *hsmp_groups[] = {
635 &hsmp_attr_grp,
636 NULL
637 };
638
639 static const struct acpi_device_id amd_hsmp_acpi_ids[] = {
640 {ACPI_HSMP_DEVICE_HID, 0},
641 {}
642 };
643 MODULE_DEVICE_TABLE(acpi, amd_hsmp_acpi_ids);
644
645 /*
646 * kref release: tear down the shared ACPI socket state once the last socket
647 * drops its reference. Deregister /dev/hsmp if it was registered, unmap any
648 * metric-table DRAM, destroy the per-socket mutexes and free the socket array.
649 *
650 * Runs from kref_put() with hsmp_sock_rwsem held for write, since the remove
651 * and probe-failure paths both drop their reference under that lock. The write
652 * lock has drained any in-flight hsmp_send_message(), so unmapping the mailbox
653 * and freeing the array cannot race the data plane.
654 */
hsmp_acpi_sock_release(struct kref * kref)655 static void hsmp_acpi_sock_release(struct kref *kref)
656 {
657 lockdep_assert_held_write(&hsmp_sock_rwsem);
658
659 if (!IS_ERR_OR_NULL(hsmp_pdev->mdev.this_device))
660 hsmp_misc_deregister();
661 hsmp_unmap_metric_tbls(hsmp_pdev);
662 hsmp_destroy_metric_read_locks(hsmp_pdev);
663 kfree(hsmp_pdev->sock);
664 hsmp_pdev->sock = NULL;
665 hsmp_pdev->num_sockets = 0;
666 hsmp_pdev->proto_ver = 0;
667 }
668
669 /**
670 * hsmp_acpi_probe_failure_cleanup() - Undo a failed ACPI socket probe.
671 * @dev: ACPI companion device whose probe failed.
672 *
673 * This device already took a reference on entry to hsmp_acpi_probe(), so clear
674 * its sock->dev and drop that reference; the shared state is released if it was
675 * the last one.
676 *
677 * Clearing sock->dev matters on multi-socket systems: when a non-first socket
678 * fails, the array stays alive (owned by an already-probed socket) and
679 * remove() is never called for this device, yet devres unmaps its mailbox once
680 * probe() returns. Without clearing dev, a later message to this index would
681 * pass every gate in hsmp_send_message() and reach the unmapped mailbox.
682 *
683 * sock is NULL if probe failed before hsmp_parse_acpi_table() set the drvdata.
684 *
685 * Called from hsmp_acpi_probe(), which already holds hsmp_sock_rwsem for write.
686 */
hsmp_acpi_probe_failure_cleanup(struct device * dev)687 static void hsmp_acpi_probe_failure_cleanup(struct device *dev)
688 {
689 struct hsmp_socket *sock = dev_get_drvdata(dev);
690
691 lockdep_assert_held_write(&hsmp_sock_rwsem);
692
693 if (sock)
694 sock->dev = NULL;
695
696 kref_put(&hsmp_acpi_sock_kref, hsmp_acpi_sock_release);
697 }
698
hsmp_acpi_probe(struct platform_device * pdev)699 static int hsmp_acpi_probe(struct platform_device *pdev)
700 {
701 int ret;
702
703 hsmp_pdev = get_hsmp_pdev();
704 if (!hsmp_pdev)
705 return -ENOMEM;
706
707 /*
708 * Multiple ACPI socket devices probe in parallel, but the one-time
709 * socket-array allocation and /dev/hsmp registration below must run
710 * exactly once. Hold the socket rwsem for write across the whole
711 * bring-up so it cannot race a concurrent probe or remove, and so the
712 * probe-failure teardown drains the data plane.
713 */
714 guard(rwsem_write)(&hsmp_sock_rwsem);
715
716 if (!hsmp_pdev->sock) {
717 hsmp_pdev->num_sockets = topology_max_packages();
718 if (!hsmp_pdev->num_sockets) {
719 dev_err(&pdev->dev, "No CPU sockets detected\n");
720 return -ENODEV;
721 }
722
723 hsmp_pdev->sock = kcalloc(hsmp_pdev->num_sockets,
724 sizeof(*hsmp_pdev->sock),
725 GFP_KERNEL);
726 if (!hsmp_pdev->sock)
727 return -ENOMEM;
728
729 hsmp_init_metric_read_locks(hsmp_pdev);
730 kref_init(&hsmp_acpi_sock_kref);
731 } else {
732 kref_get(&hsmp_acpi_sock_kref);
733 }
734
735 /*
736 * This socket now holds a reference (kref_init on the first socket,
737 * kref_get afterwards). Every failure path below drops it via
738 * hsmp_acpi_probe_failure_cleanup(), and a successful probe hands it to
739 * hsmp_acpi_remove().
740 */
741 ret = init_acpi(&pdev->dev);
742 if (ret) {
743 dev_err(&pdev->dev, "Failed to initialize HSMP interface.\n");
744 hsmp_acpi_probe_failure_cleanup(&pdev->dev);
745 return ret;
746 }
747
748 if (IS_ERR_OR_NULL(hsmp_pdev->mdev.this_device)) {
749 /*
750 * Register /dev/hsmp unparented. It is a singleton shared by all
751 * ACPI sockets and outlives all but the last of them, so
752 * parenting it to this socket's device would leave a dangling
753 * parent once that socket is unbound.
754 */
755 ret = hsmp_misc_register(NULL);
756 if (ret) {
757 dev_err(&pdev->dev, "Failed to register misc device\n");
758 hsmp_acpi_probe_failure_cleanup(&pdev->dev);
759 return ret;
760 }
761 dev_dbg(&pdev->dev, "AMD HSMP ACPI misc device registered\n");
762 }
763
764 return 0;
765 }
766
hsmp_acpi_remove(struct platform_device * pdev)767 static void hsmp_acpi_remove(struct platform_device *pdev)
768 {
769 struct hsmp_socket *sock = dev_get_drvdata(&pdev->dev);
770
771 /*
772 * Serialize the kref_put() and any release it triggers against a
773 * concurrent probe, and drain the data plane for the whole
774 * teardown: this covers the per-socket unbind, whose mailbox devres
775 * unmaps once we return, and the last unbind that frees the socket
776 * array in hsmp_acpi_sock_release().
777 */
778 guard(rwsem_write)(&hsmp_sock_rwsem);
779
780 /*
781 * Clear this socket's dev so hsmp_send_message() rejects it before
782 * devres unmaps the mailbox. On a non-final unbind the socket array
783 * stays alive, so without this a later message to this index would
784 * reach an unmapped iomem region.
785 */
786 sock->dev = NULL;
787
788 kref_put(&hsmp_acpi_sock_kref, hsmp_acpi_sock_release);
789 }
790
791 static struct platform_driver amd_hsmp_driver = {
792 .probe = hsmp_acpi_probe,
793 .remove = hsmp_acpi_remove,
794 .driver = {
795 .name = DRIVER_NAME,
796 .acpi_match_table = amd_hsmp_acpi_ids,
797 .dev_groups = hsmp_groups,
798 },
799 };
800
801 module_platform_driver(amd_hsmp_driver);
802
803 MODULE_IMPORT_NS("AMD_HSMP");
804 MODULE_DESCRIPTION("AMD HSMP Platform Interface Driver");
805 MODULE_VERSION(DRIVER_VERSION);
806 MODULE_LICENSE("GPL");
807