xref: /linux/drivers/gpu/drm/xe/xe_vram_freq.c (revision fe13fd68337512da4b7aee6326c397e817a943c7)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2024 Intel Corporation
4  */
5 #include <linux/sysfs.h>
6 #include <drm/drm_managed.h>
7 
8 #include "xe_gt_types.h"
9 #include "xe_pcode.h"
10 #include "xe_pcode_api.h"
11 #include "xe_tile.h"
12 #include "xe_tile_sysfs.h"
13 #include "xe_vram_freq.h"
14 
15 /**
16  * DOC: Xe VRAM freq
17  *
18  * Provides sysfs entries for vram frequency in tile
19  *
20  * device/tile#/memory/freq0/max_freq - This is maximum frequency. This value is read-only as it
21  *					is the fixed fuse point P0. It is not the system
22  *					configuration.
23  * device/tile#/memory/freq0/min_freq - This is minimum frequency. This value is read-only as it
24  *					is the fixed fuse point PN. It is not the system
25  *					configuration.
26  */
27 
28 static struct xe_tile *dev_to_tile(struct device *dev)
29 {
30 	return kobj_to_tile(dev->kobj.parent);
31 }
32 
33 static ssize_t max_freq_show(struct device *dev, struct device_attribute *attr,
34 			     char *buf)
35 {
36 	struct xe_tile *tile = dev_to_tile(dev);
37 	u32 val, mbox;
38 	int err;
39 
40 	mbox = REG_FIELD_PREP(PCODE_MB_COMMAND, PCODE_FREQUENCY_CONFIG)
41 		| REG_FIELD_PREP(PCODE_MB_PARAM1, PCODE_MBOX_FC_SC_READ_FUSED_P0)
42 		| REG_FIELD_PREP(PCODE_MB_PARAM2, PCODE_MBOX_DOMAIN_HBM);
43 
44 	err = xe_pcode_read(tile, mbox, &val, NULL);
45 	if (err)
46 		return err;
47 
48 	/* data_out - Fused P0 for domain ID in units of 50 MHz */
49 	val *= 50;
50 
51 	return sysfs_emit(buf, "%u\n", val);
52 }
53 static DEVICE_ATTR_RO(max_freq);
54 
55 static ssize_t min_freq_show(struct device *dev, struct device_attribute *attr,
56 			     char *buf)
57 {
58 	struct xe_tile *tile = dev_to_tile(dev);
59 	u32 val, mbox;
60 	int err;
61 
62 	mbox = REG_FIELD_PREP(PCODE_MB_COMMAND, PCODE_FREQUENCY_CONFIG)
63 		| REG_FIELD_PREP(PCODE_MB_PARAM1, PCODE_MBOX_FC_SC_READ_FUSED_PN)
64 		| REG_FIELD_PREP(PCODE_MB_PARAM2, PCODE_MBOX_DOMAIN_HBM);
65 
66 	err = xe_pcode_read(tile, mbox, &val, NULL);
67 	if (err)
68 		return err;
69 
70 	/* data_out - Fused Pn for domain ID in units of 50 MHz */
71 	val *= 50;
72 
73 	return sysfs_emit(buf, "%u\n", val);
74 }
75 static DEVICE_ATTR_RO(min_freq);
76 
77 static struct attribute *freq_attrs[] = {
78 	&dev_attr_max_freq.attr,
79 	&dev_attr_min_freq.attr,
80 	NULL
81 };
82 
83 static const struct attribute_group freq_group_attrs = {
84 	.name = "freq0",
85 	.attrs = freq_attrs,
86 };
87 
88 static void vram_freq_sysfs_fini(void *arg)
89 {
90 	struct kobject *kobj = arg;
91 
92 	sysfs_remove_group(kobj, &freq_group_attrs);
93 	kobject_put(kobj);
94 }
95 
96 /**
97  * xe_vram_freq_sysfs_init - Initialize vram frequency sysfs component
98  * @tile: Xe Tile object
99  *
100  * It needs to be initialized after the main tile component is ready
101  *
102  * Returns: 0 on success, negative error code on error.
103  */
104 int xe_vram_freq_sysfs_init(struct xe_tile *tile)
105 {
106 	struct xe_device *xe = tile_to_xe(tile);
107 	struct kobject *kobj;
108 	int err;
109 
110 	if (xe->info.platform != XE_PVC)
111 		return 0;
112 
113 	kobj = kobject_create_and_add("memory", tile->sysfs);
114 	if (!kobj)
115 		return -ENOMEM;
116 
117 	err = sysfs_create_group(kobj, &freq_group_attrs);
118 	if (err) {
119 		kobject_put(kobj);
120 		return err;
121 	}
122 
123 	return devm_add_action_or_reset(xe->drm.dev, vram_freq_sysfs_fini, kobj);
124 }
125