Skip to content

Commit e9d50e4

Browse files
Huang YiweiJassiBrar
authored andcommitted
mailbox: qcom-ipcc: Dynamic alloc for channel arrangement
Dynamic alloc for channel arrangement instead of static alloced array, it is more flexible and can reduce memory usage. Signed-off-by: Huang Yiwei <[email protected]> Signed-off-by: Jassi Brar <[email protected]>
1 parent f10b1fc commit e9d50e4

File tree

1 file changed

+69
-21
lines changed

1 file changed

+69
-21
lines changed

drivers/mailbox/qcom-ipcc.c

Lines changed: 69 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
#include <dt-bindings/mailbox/qcom-ipcc.h>
1515

16-
#define IPCC_MBOX_MAX_CHAN 48
17-
1816
/* IPCC Register offsets */
1917
#define IPCC_REG_SEND_ID 0x0c
2018
#define IPCC_REG_RECV_ID 0x10
@@ -52,9 +50,10 @@ struct qcom_ipcc {
5250
struct device *dev;
5351
void __iomem *base;
5452
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;
5755
struct mbox_controller mbox;
56+
int num_chans;
5857
int irq;
5958
};
6059

@@ -166,41 +165,87 @@ static struct mbox_chan *qcom_ipcc_mbox_xlate(struct mbox_controller *mbox,
166165
struct qcom_ipcc *ipcc = to_qcom_ipcc(mbox);
167166
struct qcom_ipcc_chan_info *mchan;
168167
struct mbox_chan *chan;
169-
unsigned int i;
168+
struct device *dev;
169+
int chan_id;
170+
171+
dev = ipcc->dev;
170172

171173
if (ph->args_count != 2)
172174
return ERR_PTR(-EINVAL);
173175

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;
183179

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);
185185
}
186186

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;
188199
}
189200

190201
static const struct mbox_chan_ops ipcc_mbox_chan_ops = {
191202
.send_data = qcom_ipcc_mbox_send_data,
192203
.shutdown = qcom_ipcc_mbox_shutdown,
193204
};
194205

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)
196208
{
209+
struct of_phandle_args curr_ph;
210+
struct device_node *client_dn;
197211
struct mbox_controller *mbox;
198212
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;
199244

200245
mbox = &ipcc->mbox;
201246
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;
204249
mbox->ops = &ipcc_mbox_chan_ops;
205250
mbox->of_xlate = qcom_ipcc_mbox_xlate;
206251
mbox->txdone_irq = false;
@@ -233,22 +278,25 @@ static int qcom_ipcc_probe(struct platform_device *pdev)
233278
if (!ipcc->irq_domain)
234279
return -ENOMEM;
235280

236-
ret = qcom_ipcc_setup_mbox(ipcc);
281+
ret = qcom_ipcc_setup_mbox(ipcc, pdev->dev.of_node);
237282
if (ret)
238283
goto err_mbox;
239284

240285
ret = devm_request_irq(&pdev->dev, ipcc->irq, qcom_ipcc_irq_fn,
241286
IRQF_TRIGGER_HIGH, "ipcc", ipcc);
242287
if (ret < 0) {
243288
dev_err(&pdev->dev, "Failed to register the irq: %d\n", ret);
244-
goto err_mbox;
289+
goto err_req_irq;
245290
}
246291

247292
enable_irq_wake(ipcc->irq);
248293
platform_set_drvdata(pdev, ipcc);
249294

250295
return 0;
251296

297+
err_req_irq:
298+
if (ipcc->num_chans)
299+
mbox_controller_unregister(&ipcc->mbox);
252300
err_mbox:
253301
irq_domain_remove(ipcc->irq_domain);
254302

0 commit comments

Comments
 (0)