|
13 | 13 |
|
14 | 14 | #include <dt-bindings/mailbox/qcom-ipcc.h> |
15 | 15 |
|
16 | | -#define IPCC_MBOX_MAX_CHAN 48 |
17 | | - |
18 | 16 | /* IPCC Register offsets */ |
19 | 17 | #define IPCC_REG_SEND_ID 0x0c |
20 | 18 | #define IPCC_REG_RECV_ID 0x10 |
@@ -52,9 +50,10 @@ struct qcom_ipcc { |
52 | 50 | struct device *dev; |
53 | 51 | void __iomem *base; |
54 | 52 | struct irq_domain *irq_domain; |
55 | | - struct mbox_chan chan[IPCC_MBOX_MAX_CHAN]; |
56 | | - struct qcom_ipcc_chan_info mchan[IPCC_MBOX_MAX_CHAN]; |
| 53 | + struct mbox_chan *chans; |
| 54 | + struct qcom_ipcc_chan_info *mchan; |
57 | 55 | struct mbox_controller mbox; |
| 56 | + int num_chans; |
58 | 57 | int irq; |
59 | 58 | }; |
60 | 59 |
|
@@ -166,41 +165,87 @@ static struct mbox_chan *qcom_ipcc_mbox_xlate(struct mbox_controller *mbox, |
166 | 165 | struct qcom_ipcc *ipcc = to_qcom_ipcc(mbox); |
167 | 166 | struct qcom_ipcc_chan_info *mchan; |
168 | 167 | struct mbox_chan *chan; |
169 | | - unsigned int i; |
| 168 | + struct device *dev; |
| 169 | + int chan_id; |
| 170 | + |
| 171 | + dev = ipcc->dev; |
170 | 172 |
|
171 | 173 | if (ph->args_count != 2) |
172 | 174 | return ERR_PTR(-EINVAL); |
173 | 175 |
|
174 | | - for (i = 0; i < IPCC_MBOX_MAX_CHAN; i++) { |
175 | | - chan = &ipcc->chan[i]; |
176 | | - if (!chan->con_priv) { |
177 | | - mchan = &ipcc->mchan[i]; |
178 | | - mchan->client_id = ph->args[0]; |
179 | | - mchan->signal_id = ph->args[1]; |
180 | | - chan->con_priv = mchan; |
181 | | - break; |
182 | | - } |
| 176 | + for (chan_id = 0; chan_id < mbox->num_chans; chan_id++) { |
| 177 | + chan = &ipcc->chans[chan_id]; |
| 178 | + mchan = chan->con_priv; |
183 | 179 |
|
184 | | - chan = NULL; |
| 180 | + if (!mchan) |
| 181 | + break; |
| 182 | + else if (mchan->client_id == ph->args[0] && |
| 183 | + mchan->signal_id == ph->args[1]) |
| 184 | + return ERR_PTR(-EBUSY); |
185 | 185 | } |
186 | 186 |
|
187 | | - return chan ?: ERR_PTR(-EBUSY); |
| 187 | + if (chan_id >= mbox->num_chans) |
| 188 | + return ERR_PTR(-EBUSY); |
| 189 | + |
| 190 | + mchan = devm_kzalloc(dev, sizeof(*mchan), GFP_KERNEL); |
| 191 | + if (!mchan) |
| 192 | + return ERR_PTR(-ENOMEM); |
| 193 | + |
| 194 | + mchan->client_id = ph->args[0]; |
| 195 | + mchan->signal_id = ph->args[1]; |
| 196 | + chan->con_priv = mchan; |
| 197 | + |
| 198 | + return chan; |
188 | 199 | } |
189 | 200 |
|
190 | 201 | static const struct mbox_chan_ops ipcc_mbox_chan_ops = { |
191 | 202 | .send_data = qcom_ipcc_mbox_send_data, |
192 | 203 | .shutdown = qcom_ipcc_mbox_shutdown, |
193 | 204 | }; |
194 | 205 |
|
195 | | -static int qcom_ipcc_setup_mbox(struct qcom_ipcc *ipcc) |
| 206 | +static int qcom_ipcc_setup_mbox(struct qcom_ipcc *ipcc, |
| 207 | + struct device_node *controller_dn) |
196 | 208 | { |
| 209 | + struct of_phandle_args curr_ph; |
| 210 | + struct device_node *client_dn; |
197 | 211 | struct mbox_controller *mbox; |
198 | 212 | struct device *dev = ipcc->dev; |
| 213 | + int i, j, ret; |
| 214 | + |
| 215 | + /* |
| 216 | + * Find out the number of clients interested in this mailbox |
| 217 | + * and create channels accordingly. |
| 218 | + */ |
| 219 | + ipcc->num_chans = 0; |
| 220 | + for_each_node_with_property(client_dn, "mboxes") { |
| 221 | + if (!of_device_is_available(client_dn)) |
| 222 | + continue; |
| 223 | + i = of_count_phandle_with_args(client_dn, |
| 224 | + "mboxes", "#mbox-cells"); |
| 225 | + for (j = 0; j < i; j++) { |
| 226 | + ret = of_parse_phandle_with_args(client_dn, "mboxes", |
| 227 | + "#mbox-cells", j, &curr_ph); |
| 228 | + of_node_put(curr_ph.np); |
| 229 | + if (!ret && curr_ph.np == controller_dn) { |
| 230 | + ipcc->num_chans++; |
| 231 | + break; |
| 232 | + } |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + /* If no clients are found, skip registering as a mbox controller */ |
| 237 | + if (!ipcc->num_chans) |
| 238 | + return 0; |
| 239 | + |
| 240 | + ipcc->chans = devm_kcalloc(dev, ipcc->num_chans, |
| 241 | + sizeof(struct mbox_chan), GFP_KERNEL); |
| 242 | + if (!ipcc->chans) |
| 243 | + return -ENOMEM; |
199 | 244 |
|
200 | 245 | mbox = &ipcc->mbox; |
201 | 246 | mbox->dev = dev; |
202 | | - mbox->num_chans = IPCC_MBOX_MAX_CHAN; |
203 | | - mbox->chans = ipcc->chan; |
| 247 | + mbox->num_chans = ipcc->num_chans; |
| 248 | + mbox->chans = ipcc->chans; |
204 | 249 | mbox->ops = &ipcc_mbox_chan_ops; |
205 | 250 | mbox->of_xlate = qcom_ipcc_mbox_xlate; |
206 | 251 | mbox->txdone_irq = false; |
@@ -233,22 +278,25 @@ static int qcom_ipcc_probe(struct platform_device *pdev) |
233 | 278 | if (!ipcc->irq_domain) |
234 | 279 | return -ENOMEM; |
235 | 280 |
|
236 | | - ret = qcom_ipcc_setup_mbox(ipcc); |
| 281 | + ret = qcom_ipcc_setup_mbox(ipcc, pdev->dev.of_node); |
237 | 282 | if (ret) |
238 | 283 | goto err_mbox; |
239 | 284 |
|
240 | 285 | ret = devm_request_irq(&pdev->dev, ipcc->irq, qcom_ipcc_irq_fn, |
241 | 286 | IRQF_TRIGGER_HIGH, "ipcc", ipcc); |
242 | 287 | if (ret < 0) { |
243 | 288 | dev_err(&pdev->dev, "Failed to register the irq: %d\n", ret); |
244 | | - goto err_mbox; |
| 289 | + goto err_req_irq; |
245 | 290 | } |
246 | 291 |
|
247 | 292 | enable_irq_wake(ipcc->irq); |
248 | 293 | platform_set_drvdata(pdev, ipcc); |
249 | 294 |
|
250 | 295 | return 0; |
251 | 296 |
|
| 297 | +err_req_irq: |
| 298 | + if (ipcc->num_chans) |
| 299 | + mbox_controller_unregister(&ipcc->mbox); |
252 | 300 | err_mbox: |
253 | 301 | irq_domain_remove(ipcc->irq_domain); |
254 | 302 |
|
|
0 commit comments