blob: 08736ea936aee7a802fb92df80c4a2c834ca8128 [file] [log] [blame]
Caleb Connolly90ff5812024-07-15 12:08:02 +02001/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Copyright (c) 2016-2018, 2020, The Linux Foundation. All rights reserved.
4 * Copyright (c) 2024, Qualcomm Innovation Center, Inc. All rights reserved.
5 */
6
Caleb Connolly31edd022024-07-15 12:08:03 +02007#define pr_fmt(fmt) "cmd-db: " fmt
8
9#include <dm.h>
10#include <dm/ofnode.h>
11#include <dm/device_compat.h>
Caleb Connolly90ff5812024-07-15 12:08:02 +020012#include <linux/kernel.h>
Caleb Connolly90ff5812024-07-15 12:08:02 +020013#include <linux/types.h>
Caleb Connolly31edd022024-07-15 12:08:03 +020014#include <linux/ioport.h>
15#include <linux/byteorder/generic.h>
Caleb Connolly90ff5812024-07-15 12:08:02 +020016
17#include <soc/qcom/cmd-db.h>
18
19#define NUM_PRIORITY 2
20#define MAX_SLV_ID 8
21#define SLAVE_ID_MASK 0x7
22#define SLAVE_ID_SHIFT 16
23#define SLAVE_ID(addr) FIELD_GET(GENMASK(19, 16), addr)
24#define VRM_ADDR(addr) FIELD_GET(GENMASK(19, 4), addr)
25
26/**
27 * struct entry_header: header for each entry in cmddb
28 *
29 * @id: resource's identifier
30 * @priority: unused
31 * @addr: the address of the resource
32 * @len: length of the data
33 * @offset: offset from :@data_offset, start of the data
34 */
35struct entry_header {
36 u8 id[8];
37 __le32 priority[NUM_PRIORITY];
38 __le32 addr;
39 __le16 len;
40 __le16 offset;
41};
42
43/**
44 * struct rsc_hdr: resource header information
45 *
46 * @slv_id: id for the resource
47 * @header_offset: entry's header at offset from the end of the cmd_db_header
48 * @data_offset: entry's data at offset from the end of the cmd_db_header
49 * @cnt: number of entries for HW type
50 * @version: MSB is major, LSB is minor
51 * @reserved: reserved for future use.
52 */
53struct rsc_hdr {
54 __le16 slv_id;
55 __le16 header_offset;
56 __le16 data_offset;
57 __le16 cnt;
58 __le16 version;
59 __le16 reserved[3];
60};
61
62/**
63 * struct cmd_db_header: The DB header information
64 *
65 * @version: The cmd db version
66 * @magic: constant expected in the database
67 * @header: array of resources
68 * @checksum: checksum for the header. Unused.
69 * @reserved: reserved memory
70 * @data: driver specific data
71 */
72struct cmd_db_header {
73 __le32 version;
74 u8 magic[4];
75 struct rsc_hdr header[MAX_SLV_ID];
76 __le32 checksum;
77 __le32 reserved;
78 u8 data[];
79};
80
81/**
82 * DOC: Description of the Command DB database.
83 *
84 * At the start of the command DB memory is the cmd_db_header structure.
85 * The cmd_db_header holds the version, checksum, magic key as well as an
86 * array for header for each slave (depicted by the rsc_header). Each h/w
87 * based accelerator is a 'slave' (shared resource) and has slave id indicating
88 * the type of accelerator. The rsc_header is the header for such individual
89 * slaves of a given type. The entries for each of these slaves begin at the
90 * rsc_hdr.header_offset. In addition each slave could have auxiliary data
91 * that may be needed by the driver. The data for the slave starts at the
92 * entry_header.offset to the location pointed to by the rsc_hdr.data_offset.
93 *
94 * Drivers have a stringified key to a slave/resource. They can query the slave
95 * information and get the slave id and the auxiliary data and the length of the
96 * data. Using this information, they can format the request to be sent to the
97 * h/w accelerator and request a resource state.
98 */
99
100static const u8 CMD_DB_MAGIC[] = { 0xdb, 0x30, 0x03, 0x0c };
101
102static bool cmd_db_magic_matches(const struct cmd_db_header *header)
103{
104 const u8 *magic = header->magic;
105
106 return memcmp(magic, CMD_DB_MAGIC, ARRAY_SIZE(CMD_DB_MAGIC)) == 0;
107}
108
Caleb Connollyf8bd3bb2024-07-15 12:08:05 +0200109static struct cmd_db_header *cmd_db_header __section(".data") = NULL;
Caleb Connolly90ff5812024-07-15 12:08:02 +0200110
111static inline const void *rsc_to_entry_header(const struct rsc_hdr *hdr)
112{
113 u16 offset = le16_to_cpu(hdr->header_offset);
114
115 return cmd_db_header->data + offset;
116}
117
118static inline void *
119rsc_offset(const struct rsc_hdr *hdr, const struct entry_header *ent)
120{
121 u16 offset = le16_to_cpu(hdr->data_offset);
122 u16 loffset = le16_to_cpu(ent->offset);
123
124 return cmd_db_header->data + offset + loffset;
125}
126
Caleb Connolly90ff5812024-07-15 12:08:02 +0200127static int cmd_db_get_header(const char *id, const struct entry_header **eh,
128 const struct rsc_hdr **rh)
129{
130 const struct rsc_hdr *rsc_hdr;
131 const struct entry_header *ent;
Caleb Connollyb005a242024-07-15 12:08:06 +0200132 int i, j;
Caleb Connolly90ff5812024-07-15 12:08:02 +0200133 u8 query[sizeof(ent->id)] __nonstring;
134
Caleb Connollyb005a242024-07-15 12:08:06 +0200135 strncpy(query, id, sizeof(query));
Caleb Connolly90ff5812024-07-15 12:08:02 +0200136
137 for (i = 0; i < MAX_SLV_ID; i++) {
138 rsc_hdr = &cmd_db_header->header[i];
139 if (!rsc_hdr->slv_id)
140 break;
141
142 ent = rsc_to_entry_header(rsc_hdr);
143 for (j = 0; j < le16_to_cpu(rsc_hdr->cnt); j++, ent++) {
144 if (memcmp(ent->id, query, sizeof(ent->id)) == 0) {
145 if (eh)
146 *eh = ent;
147 if (rh)
148 *rh = rsc_hdr;
149 return 0;
150 }
151 }
152 }
153
154 return -ENODEV;
155}
156
157/**
158 * cmd_db_read_addr() - Query command db for resource id address.
159 *
160 * @id: resource id to query for address
161 *
162 * Return: resource address on success, 0 on error
163 *
164 * This is used to retrieve resource address based on resource
165 * id.
166 */
167u32 cmd_db_read_addr(const char *id)
168{
169 int ret;
170 const struct entry_header *ent;
171
Caleb Connollyb005a242024-07-15 12:08:06 +0200172 debug("%s(%s)\n", __func__, id);
173
174 if (!cmd_db_header) {
175 log_err("%s: Command DB not initialized\n", __func__);
176 return 0;
177 }
178
Caleb Connolly90ff5812024-07-15 12:08:02 +0200179 ret = cmd_db_get_header(id, &ent, NULL);
180
181 return ret < 0 ? 0 : le32_to_cpu(ent->addr);
182}
183EXPORT_SYMBOL_GPL(cmd_db_read_addr);
184
Caleb Connollyf8bd3bb2024-07-15 12:08:05 +0200185int cmd_db_bind(struct udevice *dev)
Caleb Connolly90ff5812024-07-15 12:08:02 +0200186{
Caleb Connollyf8bd3bb2024-07-15 12:08:05 +0200187 void __iomem *base;
188 ofnode node;
Caleb Connolly90ff5812024-07-15 12:08:02 +0200189
Caleb Connollyf8bd3bb2024-07-15 12:08:05 +0200190 if (cmd_db_header)
191 return 0;
Caleb Connolly90ff5812024-07-15 12:08:02 +0200192
Caleb Connollyf8bd3bb2024-07-15 12:08:05 +0200193 node = dev_ofnode(dev);
194
195 debug("%s(%s)\n", __func__, ofnode_get_name(node));
196
197 base = (void __iomem *)ofnode_get_addr(node);
198 if ((fdt_addr_t)base == FDT_ADDR_T_NONE) {
199 log_err("%s: Failed to read base address\n", __func__);
200 return -ENOENT;
Caleb Connolly90ff5812024-07-15 12:08:02 +0200201 }
202
Caleb Connollyf8bd3bb2024-07-15 12:08:05 +0200203 cmd_db_header = base;
Caleb Connolly90ff5812024-07-15 12:08:02 +0200204 if (!cmd_db_magic_matches(cmd_db_header)) {
Caleb Connollyf8bd3bb2024-07-15 12:08:05 +0200205 log_err("%s: Invalid Command DB Magic\n", __func__);
Caleb Connolly90ff5812024-07-15 12:08:02 +0200206 return -EINVAL;
207 }
208
Caleb Connolly90ff5812024-07-15 12:08:02 +0200209 return 0;
210}
211
Caleb Connollyf8bd3bb2024-07-15 12:08:05 +0200212static const struct udevice_id cmd_db_ids[] = {
Caleb Connolly90ff5812024-07-15 12:08:02 +0200213 { .compatible = "qcom,cmd-db" },
214 { }
215};
Caleb Connolly90ff5812024-07-15 12:08:02 +0200216
Caleb Connollyf8bd3bb2024-07-15 12:08:05 +0200217U_BOOT_DRIVER(qcom_cmd_db) = {
218 .name = "qcom_cmd_db",
219 .id = UCLASS_MISC,
Caleb Connollyb005a242024-07-15 12:08:06 +0200220 .bind = cmd_db_bind,
Caleb Connollyf8bd3bb2024-07-15 12:08:05 +0200221 .of_match = cmd_db_ids,
Caleb Connolly90ff5812024-07-15 12:08:02 +0200222};
223
Caleb Connolly90ff5812024-07-15 12:08:02 +0200224MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Command DB Driver");
225MODULE_LICENSE("GPL v2");