pcc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2014 Linaro Ltd.
  4. * Author: Ashwin Chaugule <ashwin.chaugule@linaro.org>
  5. *
  6. * PCC (Platform Communication Channel) is defined in the ACPI 5.0+
  7. * specification. It is a mailbox like mechanism to allow clients
  8. * such as CPPC (Collaborative Processor Performance Control), RAS
  9. * (Reliability, Availability and Serviceability) and MPST (Memory
  10. * Node Power State Table) to talk to the platform (e.g. BMC) through
  11. * shared memory regions as defined in the PCC table entries. The PCC
  12. * specification supports a Doorbell mechanism for the PCC clients
  13. * to notify the platform about new data. This Doorbell information
  14. * is also specified in each PCC table entry.
  15. *
  16. * Typical high level flow of operation is:
  17. *
  18. * PCC Reads:
  19. * * Client tries to acquire a channel lock.
  20. * * After it is acquired it writes READ cmd in communication region cmd
  21. * address.
  22. * * Client issues mbox_send_message() which rings the PCC doorbell
  23. * for its PCC channel.
  24. * * If command completes, then client has control over channel and
  25. * it can proceed with its reads.
  26. * * Client releases lock.
  27. *
  28. * PCC Writes:
  29. * * Client tries to acquire channel lock.
  30. * * Client writes to its communication region after it acquires a
  31. * channel lock.
  32. * * Client writes WRITE cmd in communication region cmd address.
  33. * * Client issues mbox_send_message() which rings the PCC doorbell
  34. * for its PCC channel.
  35. * * If command completes, then writes have succeeded and it can release
  36. * the channel lock.
  37. *
  38. * There is a Nominal latency defined for each channel which indicates
  39. * how long to wait until a command completes. If command is not complete
  40. * the client needs to retry or assume failure.
  41. *
  42. * For more details about PCC, please see the ACPI specification from
  43. * http://www.uefi.org/ACPIv5.1 Section 14.
  44. *
  45. * This file implements PCC as a Mailbox controller and allows for PCC
  46. * clients to be implemented as its Mailbox Client Channels.
  47. */
  48. #include <linux/acpi.h>
  49. #include <linux/delay.h>
  50. #include <linux/io.h>
  51. #include <linux/init.h>
  52. #include <linux/interrupt.h>
  53. #include <linux/list.h>
  54. #include <linux/log2.h>
  55. #include <linux/platform_device.h>
  56. #include <linux/mailbox_controller.h>
  57. #include <linux/mailbox_client.h>
  58. #include <linux/io-64-nonatomic-lo-hi.h>
  59. #include <acpi/pcc.h>
  60. #include "mailbox.h"
  61. #define MBOX_IRQ_NAME "pcc-mbox"
  62. /**
  63. * struct pcc_chan_reg - PCC register bundle
  64. *
  65. * @vaddr: cached virtual address for this register
  66. * @gas: pointer to the generic address structure for this register
  67. * @preserve_mask: bitmask to preserve when writing to this register
  68. * @set_mask: bitmask to set when writing to this register
  69. * @status_mask: bitmask to determine and/or update the status for this register
  70. */
  71. struct pcc_chan_reg {
  72. void __iomem *vaddr;
  73. struct acpi_generic_address *gas;
  74. u64 preserve_mask;
  75. u64 set_mask;
  76. u64 status_mask;
  77. };
  78. /**
  79. * struct pcc_chan_info - PCC channel specific information
  80. *
  81. * @chan: PCC channel information with Shared Memory Region info
  82. * @db: PCC register bundle for the doorbell register
  83. * @plat_irq_ack: PCC register bundle for the platform interrupt acknowledge
  84. * register
  85. * @cmd_complete: PCC register bundle for the command complete check register
  86. * @cmd_update: PCC register bundle for the command complete update register
  87. * @error: PCC register bundle for the error status register
  88. * @plat_irq: platform interrupt
  89. * @type: PCC subspace type
  90. * @plat_irq_flags: platform interrupt flags
  91. * @chan_in_use: this flag is used just to check if the interrupt needs
  92. * handling when it is shared. Since only one transfer can occur
  93. * at a time and mailbox takes care of locking, this flag can be
  94. * accessed without a lock. Note: the type only support the
  95. * communication from OSPM to Platform, like type3, use it, and
  96. * other types completely ignore it.
  97. */
  98. struct pcc_chan_info {
  99. struct pcc_mbox_chan chan;
  100. struct pcc_chan_reg db;
  101. struct pcc_chan_reg plat_irq_ack;
  102. struct pcc_chan_reg cmd_complete;
  103. struct pcc_chan_reg cmd_update;
  104. struct pcc_chan_reg error;
  105. int plat_irq;
  106. u8 type;
  107. unsigned int plat_irq_flags;
  108. bool chan_in_use;
  109. };
  110. #define to_pcc_chan_info(c) container_of(c, struct pcc_chan_info, chan)
  111. static struct pcc_chan_info *chan_info;
  112. static int pcc_chan_count;
  113. static int pcc_send_data(struct mbox_chan *chan, void *data);
  114. /*
  115. * PCC can be used with perf critical drivers such as CPPC
  116. * So it makes sense to locally cache the virtual address and
  117. * use it to read/write to PCC registers such as doorbell register
  118. *
  119. * The below read_register and write_registers are used to read and
  120. * write from perf critical registers such as PCC doorbell register
  121. */
  122. static void read_register(void __iomem *vaddr, u64 *val, unsigned int bit_width)
  123. {
  124. switch (bit_width) {
  125. case 8:
  126. *val = readb(vaddr);
  127. break;
  128. case 16:
  129. *val = readw(vaddr);
  130. break;
  131. case 32:
  132. *val = readl(vaddr);
  133. break;
  134. case 64:
  135. *val = readq(vaddr);
  136. break;
  137. }
  138. }
  139. static void write_register(void __iomem *vaddr, u64 val, unsigned int bit_width)
  140. {
  141. switch (bit_width) {
  142. case 8:
  143. writeb(val, vaddr);
  144. break;
  145. case 16:
  146. writew(val, vaddr);
  147. break;
  148. case 32:
  149. writel(val, vaddr);
  150. break;
  151. case 64:
  152. writeq(val, vaddr);
  153. break;
  154. }
  155. }
  156. static int pcc_chan_reg_read(struct pcc_chan_reg *reg, u64 *val)
  157. {
  158. int ret = 0;
  159. if (!reg->gas) {
  160. *val = 0;
  161. return 0;
  162. }
  163. if (reg->vaddr)
  164. read_register(reg->vaddr, val, reg->gas->bit_width);
  165. else
  166. ret = acpi_read(val, reg->gas);
  167. return ret;
  168. }
  169. static int pcc_chan_reg_write(struct pcc_chan_reg *reg, u64 val)
  170. {
  171. int ret = 0;
  172. if (!reg->gas)
  173. return 0;
  174. if (reg->vaddr)
  175. write_register(reg->vaddr, val, reg->gas->bit_width);
  176. else
  177. ret = acpi_write(val, reg->gas);
  178. return ret;
  179. }
  180. static int pcc_chan_reg_read_modify_write(struct pcc_chan_reg *reg)
  181. {
  182. int ret = 0;
  183. u64 val;
  184. ret = pcc_chan_reg_read(reg, &val);
  185. if (ret)
  186. return ret;
  187. val &= reg->preserve_mask;
  188. val |= reg->set_mask;
  189. return pcc_chan_reg_write(reg, val);
  190. }
  191. /**
  192. * pcc_map_interrupt - Map a PCC subspace GSI to a linux IRQ number
  193. * @interrupt: GSI number.
  194. * @flags: interrupt flags
  195. *
  196. * Returns: a valid linux IRQ number on success
  197. * 0 or -EINVAL on failure
  198. */
  199. static int pcc_map_interrupt(u32 interrupt, u32 flags)
  200. {
  201. int trigger, polarity;
  202. if (!interrupt)
  203. return 0;
  204. trigger = (flags & ACPI_PCCT_INTERRUPT_MODE) ? ACPI_EDGE_SENSITIVE
  205. : ACPI_LEVEL_SENSITIVE;
  206. polarity = (flags & ACPI_PCCT_INTERRUPT_POLARITY) ? ACPI_ACTIVE_LOW
  207. : ACPI_ACTIVE_HIGH;
  208. return acpi_register_gsi(NULL, interrupt, trigger, polarity);
  209. }
  210. static bool pcc_chan_plat_irq_can_be_shared(struct pcc_chan_info *pchan)
  211. {
  212. return (pchan->plat_irq_flags & ACPI_PCCT_INTERRUPT_MODE) ==
  213. ACPI_LEVEL_SENSITIVE;
  214. }
  215. static bool pcc_mbox_cmd_complete_check(struct pcc_chan_info *pchan)
  216. {
  217. u64 val;
  218. int ret;
  219. ret = pcc_chan_reg_read(&pchan->cmd_complete, &val);
  220. if (ret)
  221. return false;
  222. if (!pchan->cmd_complete.gas)
  223. return true;
  224. /*
  225. * Judge if the channel respond the interrupt based on the value of
  226. * command complete.
  227. */
  228. val &= pchan->cmd_complete.status_mask;
  229. /*
  230. * If this is PCC slave subspace channel, and the command complete
  231. * bit 0 indicates that Platform is sending a notification and OSPM
  232. * needs to respond this interrupt to process this command.
  233. */
  234. if (pchan->type == ACPI_PCCT_TYPE_EXT_PCC_SLAVE_SUBSPACE)
  235. return !val;
  236. return !!val;
  237. }
  238. static void check_and_ack(struct pcc_chan_info *pchan, struct mbox_chan *chan)
  239. {
  240. struct acpi_pcct_ext_pcc_shared_memory pcc_hdr;
  241. if (pchan->type != ACPI_PCCT_TYPE_EXT_PCC_SLAVE_SUBSPACE)
  242. return;
  243. /* If the memory region has not been mapped, we cannot
  244. * determine if we need to send the message, but we still
  245. * need to set the cmd_update flag before returning.
  246. */
  247. if (pchan->chan.shmem == NULL) {
  248. pcc_chan_reg_read_modify_write(&pchan->cmd_update);
  249. return;
  250. }
  251. memcpy_fromio(&pcc_hdr, pchan->chan.shmem,
  252. sizeof(struct acpi_pcct_ext_pcc_shared_memory));
  253. /*
  254. * The PCC slave subspace channel needs to set the command complete bit
  255. * after processing message. If the PCC_ACK_FLAG is set, it should also
  256. * ring the doorbell.
  257. *
  258. * The PCC master subspace channel clears chan_in_use to free channel.
  259. */
  260. if (le32_to_cpup(&pcc_hdr.flags) & PCC_ACK_FLAG_MASK)
  261. pcc_send_data(chan, NULL);
  262. else
  263. pcc_chan_reg_read_modify_write(&pchan->cmd_update);
  264. }
  265. /**
  266. * pcc_mbox_irq - PCC mailbox interrupt handler
  267. * @irq: interrupt number
  268. * @p: data/cookie passed from the caller to identify the channel
  269. *
  270. * Returns: IRQ_HANDLED if interrupt is handled or IRQ_NONE if not
  271. */
  272. static irqreturn_t pcc_mbox_irq(int irq, void *p)
  273. {
  274. struct pcc_chan_info *pchan;
  275. struct mbox_chan *chan = p;
  276. u64 val;
  277. int ret;
  278. pchan = chan->con_priv;
  279. if (pchan->type == ACPI_PCCT_TYPE_EXT_PCC_MASTER_SUBSPACE &&
  280. !pchan->chan_in_use)
  281. return IRQ_NONE;
  282. if (!pcc_mbox_cmd_complete_check(pchan))
  283. return IRQ_NONE;
  284. ret = pcc_chan_reg_read(&pchan->error, &val);
  285. if (ret)
  286. return IRQ_NONE;
  287. val &= pchan->error.status_mask;
  288. if (val) {
  289. val &= ~pchan->error.status_mask;
  290. pcc_chan_reg_write(&pchan->error, val);
  291. return IRQ_NONE;
  292. }
  293. if (pcc_chan_reg_read_modify_write(&pchan->plat_irq_ack))
  294. return IRQ_NONE;
  295. mbox_chan_received_data(chan, NULL);
  296. check_and_ack(pchan, chan);
  297. pchan->chan_in_use = false;
  298. return IRQ_HANDLED;
  299. }
  300. /**
  301. * pcc_mbox_request_channel - PCC clients call this function to
  302. * request a pointer to their PCC subspace, from which they
  303. * can get the details of communicating with the remote.
  304. * @cl: Pointer to Mailbox client, so we know where to bind the
  305. * Channel.
  306. * @subspace_id: The PCC Subspace index as parsed in the PCC client
  307. * ACPI package. This is used to lookup the array of PCC
  308. * subspaces as parsed by the PCC Mailbox controller.
  309. *
  310. * Return: Pointer to the PCC Mailbox Channel if successful or ERR_PTR.
  311. */
  312. struct pcc_mbox_chan *
  313. pcc_mbox_request_channel(struct mbox_client *cl, int subspace_id)
  314. {
  315. struct pcc_chan_info *pchan;
  316. struct mbox_chan *chan;
  317. int rc;
  318. if (subspace_id < 0 || subspace_id >= pcc_chan_count)
  319. return ERR_PTR(-ENOENT);
  320. pchan = chan_info + subspace_id;
  321. chan = pchan->chan.mchan;
  322. if (IS_ERR(chan) || chan->cl) {
  323. pr_err("Channel not found for idx: %d\n", subspace_id);
  324. return ERR_PTR(-EBUSY);
  325. }
  326. rc = mbox_bind_client(chan, cl);
  327. if (rc)
  328. return ERR_PTR(rc);
  329. return &pchan->chan;
  330. }
  331. EXPORT_SYMBOL_GPL(pcc_mbox_request_channel);
  332. /**
  333. * pcc_mbox_free_channel - Clients call this to free their Channel.
  334. *
  335. * @pchan: Pointer to the PCC mailbox channel as returned by
  336. * pcc_mbox_request_channel()
  337. */
  338. void pcc_mbox_free_channel(struct pcc_mbox_chan *pchan)
  339. {
  340. struct mbox_chan *chan = pchan->mchan;
  341. struct pcc_chan_info *pchan_info;
  342. struct pcc_mbox_chan *pcc_mbox_chan;
  343. if (!chan || !chan->cl)
  344. return;
  345. pchan_info = chan->con_priv;
  346. pcc_mbox_chan = &pchan_info->chan;
  347. if (pcc_mbox_chan->shmem) {
  348. iounmap(pcc_mbox_chan->shmem);
  349. pcc_mbox_chan->shmem = NULL;
  350. }
  351. mbox_free_channel(chan);
  352. }
  353. EXPORT_SYMBOL_GPL(pcc_mbox_free_channel);
  354. int pcc_mbox_ioremap(struct mbox_chan *chan)
  355. {
  356. struct pcc_chan_info *pchan_info;
  357. struct pcc_mbox_chan *pcc_mbox_chan;
  358. if (!chan || !chan->cl)
  359. return -1;
  360. pchan_info = chan->con_priv;
  361. pcc_mbox_chan = &pchan_info->chan;
  362. pcc_mbox_chan->shmem = ioremap(pcc_mbox_chan->shmem_base_addr,
  363. pcc_mbox_chan->shmem_size);
  364. return 0;
  365. }
  366. EXPORT_SYMBOL_GPL(pcc_mbox_ioremap);
  367. /**
  368. * pcc_send_data - Called from Mailbox Controller code. Used
  369. * here only to ring the channel doorbell. The PCC client
  370. * specific read/write is done in the client driver in
  371. * order to maintain atomicity over PCC channel once
  372. * OS has control over it. See above for flow of operations.
  373. * @chan: Pointer to Mailbox channel over which to send data.
  374. * @data: Client specific data written over channel. Used here
  375. * only for debug after PCC transaction completes.
  376. *
  377. * Return: Err if something failed else 0 for success.
  378. */
  379. static int pcc_send_data(struct mbox_chan *chan, void *data)
  380. {
  381. int ret;
  382. struct pcc_chan_info *pchan = chan->con_priv;
  383. ret = pcc_chan_reg_read_modify_write(&pchan->cmd_update);
  384. if (ret)
  385. return ret;
  386. ret = pcc_chan_reg_read_modify_write(&pchan->db);
  387. if (!ret && pchan->plat_irq > 0)
  388. pchan->chan_in_use = true;
  389. return ret;
  390. }
  391. /**
  392. * pcc_startup - Called from Mailbox Controller code. Used here
  393. * to request the interrupt.
  394. * @chan: Pointer to Mailbox channel to startup.
  395. *
  396. * Return: Err if something failed else 0 for success.
  397. */
  398. static int pcc_startup(struct mbox_chan *chan)
  399. {
  400. struct pcc_chan_info *pchan = chan->con_priv;
  401. unsigned long irqflags;
  402. int rc;
  403. if (pchan->plat_irq > 0) {
  404. irqflags = pcc_chan_plat_irq_can_be_shared(pchan) ?
  405. IRQF_SHARED | IRQF_ONESHOT : 0;
  406. rc = devm_request_irq(chan->mbox->dev, pchan->plat_irq, pcc_mbox_irq,
  407. irqflags, MBOX_IRQ_NAME, chan);
  408. if (unlikely(rc)) {
  409. dev_err(chan->mbox->dev, "failed to register PCC interrupt %d\n",
  410. pchan->plat_irq);
  411. return rc;
  412. }
  413. }
  414. return 0;
  415. }
  416. /**
  417. * pcc_shutdown - Called from Mailbox Controller code. Used here
  418. * to free the interrupt.
  419. * @chan: Pointer to Mailbox channel to shutdown.
  420. */
  421. static void pcc_shutdown(struct mbox_chan *chan)
  422. {
  423. struct pcc_chan_info *pchan = chan->con_priv;
  424. if (pchan->plat_irq > 0)
  425. devm_free_irq(chan->mbox->dev, pchan->plat_irq, chan);
  426. }
  427. static const struct mbox_chan_ops pcc_chan_ops = {
  428. .send_data = pcc_send_data,
  429. .startup = pcc_startup,
  430. .shutdown = pcc_shutdown,
  431. };
  432. /**
  433. * parse_pcc_subspace - Count PCC subspaces defined
  434. * @header: Pointer to the ACPI subtable header under the PCCT.
  435. * @end: End of subtable entry.
  436. *
  437. * Return: If we find a PCC subspace entry of a valid type, return 0.
  438. * Otherwise, return -EINVAL.
  439. *
  440. * This gets called for each entry in the PCC table.
  441. */
  442. static int parse_pcc_subspace(union acpi_subtable_headers *header,
  443. const unsigned long end)
  444. {
  445. struct acpi_pcct_subspace *ss = (struct acpi_pcct_subspace *) header;
  446. if (ss->header.type < ACPI_PCCT_TYPE_RESERVED)
  447. return 0;
  448. return -EINVAL;
  449. }
  450. static int
  451. pcc_chan_reg_init(struct pcc_chan_reg *reg, struct acpi_generic_address *gas,
  452. u64 preserve_mask, u64 set_mask, u64 status_mask, char *name)
  453. {
  454. if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
  455. if (!(gas->bit_width >= 8 && gas->bit_width <= 64 &&
  456. is_power_of_2(gas->bit_width))) {
  457. pr_err("Error: Cannot access register of %u bit width",
  458. gas->bit_width);
  459. return -EFAULT;
  460. }
  461. reg->vaddr = acpi_os_ioremap(gas->address, gas->bit_width / 8);
  462. if (!reg->vaddr) {
  463. pr_err("Failed to ioremap PCC %s register\n", name);
  464. return -ENOMEM;
  465. }
  466. }
  467. reg->gas = gas;
  468. reg->preserve_mask = preserve_mask;
  469. reg->set_mask = set_mask;
  470. reg->status_mask = status_mask;
  471. return 0;
  472. }
  473. /**
  474. * pcc_parse_subspace_irq - Parse the PCC IRQ and PCC ACK register
  475. *
  476. * @pchan: Pointer to the PCC channel info structure.
  477. * @pcct_entry: Pointer to the ACPI subtable header.
  478. *
  479. * Return: 0 for Success, else errno.
  480. *
  481. * There should be one entry per PCC channel. This gets called for each
  482. * entry in the PCC table. This uses PCCY Type1 structure for all applicable
  483. * types(Type 1-4) to fetch irq
  484. */
  485. static int pcc_parse_subspace_irq(struct pcc_chan_info *pchan,
  486. struct acpi_subtable_header *pcct_entry)
  487. {
  488. int ret = 0;
  489. struct acpi_pcct_hw_reduced *pcct_ss;
  490. if (pcct_entry->type < ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE ||
  491. pcct_entry->type > ACPI_PCCT_TYPE_EXT_PCC_SLAVE_SUBSPACE)
  492. return 0;
  493. pcct_ss = (struct acpi_pcct_hw_reduced *)pcct_entry;
  494. pchan->plat_irq = pcc_map_interrupt(pcct_ss->platform_interrupt,
  495. (u32)pcct_ss->flags);
  496. if (pchan->plat_irq <= 0) {
  497. pr_err("PCC GSI %d not registered\n",
  498. pcct_ss->platform_interrupt);
  499. return -EINVAL;
  500. }
  501. pchan->plat_irq_flags = pcct_ss->flags;
  502. if (pcct_ss->header.type == ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE_TYPE2) {
  503. struct acpi_pcct_hw_reduced_type2 *pcct2_ss = (void *)pcct_ss;
  504. ret = pcc_chan_reg_init(&pchan->plat_irq_ack,
  505. &pcct2_ss->platform_ack_register,
  506. pcct2_ss->ack_preserve_mask,
  507. pcct2_ss->ack_write_mask, 0,
  508. "PLAT IRQ ACK");
  509. } else if (pcct_ss->header.type == ACPI_PCCT_TYPE_EXT_PCC_MASTER_SUBSPACE ||
  510. pcct_ss->header.type == ACPI_PCCT_TYPE_EXT_PCC_SLAVE_SUBSPACE) {
  511. struct acpi_pcct_ext_pcc_master *pcct_ext = (void *)pcct_ss;
  512. ret = pcc_chan_reg_init(&pchan->plat_irq_ack,
  513. &pcct_ext->platform_ack_register,
  514. pcct_ext->ack_preserve_mask,
  515. pcct_ext->ack_set_mask, 0,
  516. "PLAT IRQ ACK");
  517. }
  518. if (pcc_chan_plat_irq_can_be_shared(pchan) &&
  519. !pchan->plat_irq_ack.gas) {
  520. pr_err("PCC subspace has level IRQ with no ACK register\n");
  521. return -EINVAL;
  522. }
  523. return ret;
  524. }
  525. /**
  526. * pcc_parse_subspace_db_reg - Parse the PCC doorbell register
  527. *
  528. * @pchan: Pointer to the PCC channel info structure.
  529. * @pcct_entry: Pointer to the ACPI subtable header.
  530. *
  531. * Return: 0 for Success, else errno.
  532. */
  533. static int pcc_parse_subspace_db_reg(struct pcc_chan_info *pchan,
  534. struct acpi_subtable_header *pcct_entry)
  535. {
  536. int ret = 0;
  537. if (pcct_entry->type <= ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE_TYPE2) {
  538. struct acpi_pcct_subspace *pcct_ss;
  539. pcct_ss = (struct acpi_pcct_subspace *)pcct_entry;
  540. ret = pcc_chan_reg_init(&pchan->db,
  541. &pcct_ss->doorbell_register,
  542. pcct_ss->preserve_mask,
  543. pcct_ss->write_mask, 0, "Doorbell");
  544. } else {
  545. struct acpi_pcct_ext_pcc_master *pcct_ext;
  546. pcct_ext = (struct acpi_pcct_ext_pcc_master *)pcct_entry;
  547. ret = pcc_chan_reg_init(&pchan->db,
  548. &pcct_ext->doorbell_register,
  549. pcct_ext->preserve_mask,
  550. pcct_ext->write_mask, 0, "Doorbell");
  551. if (ret)
  552. return ret;
  553. ret = pcc_chan_reg_init(&pchan->cmd_complete,
  554. &pcct_ext->cmd_complete_register,
  555. 0, 0, pcct_ext->cmd_complete_mask,
  556. "Command Complete Check");
  557. if (ret)
  558. return ret;
  559. ret = pcc_chan_reg_init(&pchan->cmd_update,
  560. &pcct_ext->cmd_update_register,
  561. pcct_ext->cmd_update_preserve_mask,
  562. pcct_ext->cmd_update_set_mask, 0,
  563. "Command Complete Update");
  564. if (ret)
  565. return ret;
  566. ret = pcc_chan_reg_init(&pchan->error,
  567. &pcct_ext->error_status_register,
  568. 0, 0, pcct_ext->error_status_mask,
  569. "Error Status");
  570. }
  571. return ret;
  572. }
  573. /**
  574. * pcc_parse_subspace_shmem - Parse the PCC Shared Memory Region information
  575. *
  576. * @pchan: Pointer to the PCC channel info structure.
  577. * @pcct_entry: Pointer to the ACPI subtable header.
  578. *
  579. */
  580. static void pcc_parse_subspace_shmem(struct pcc_chan_info *pchan,
  581. struct acpi_subtable_header *pcct_entry)
  582. {
  583. if (pcct_entry->type <= ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE_TYPE2) {
  584. struct acpi_pcct_subspace *pcct_ss =
  585. (struct acpi_pcct_subspace *)pcct_entry;
  586. pchan->chan.shmem_base_addr = pcct_ss->base_address;
  587. pchan->chan.shmem_size = pcct_ss->length;
  588. pchan->chan.latency = pcct_ss->latency;
  589. pchan->chan.max_access_rate = pcct_ss->max_access_rate;
  590. pchan->chan.min_turnaround_time = pcct_ss->min_turnaround_time;
  591. } else {
  592. struct acpi_pcct_ext_pcc_master *pcct_ext =
  593. (struct acpi_pcct_ext_pcc_master *)pcct_entry;
  594. pchan->chan.shmem_base_addr = pcct_ext->base_address;
  595. pchan->chan.shmem_size = pcct_ext->length;
  596. pchan->chan.latency = pcct_ext->latency;
  597. pchan->chan.max_access_rate = pcct_ext->max_access_rate;
  598. pchan->chan.min_turnaround_time = pcct_ext->min_turnaround_time;
  599. }
  600. }
  601. /**
  602. * acpi_pcc_probe - Parse the ACPI tree for the PCCT.
  603. *
  604. * Return: 0 for Success, else errno.
  605. */
  606. static int __init acpi_pcc_probe(void)
  607. {
  608. int count, i, rc = 0;
  609. acpi_status status;
  610. struct acpi_table_header *pcct_tbl;
  611. struct acpi_subtable_proc proc[ACPI_PCCT_TYPE_RESERVED];
  612. status = acpi_get_table(ACPI_SIG_PCCT, 0, &pcct_tbl);
  613. if (ACPI_FAILURE(status) || !pcct_tbl)
  614. return -ENODEV;
  615. /* Set up the subtable handlers */
  616. for (i = ACPI_PCCT_TYPE_GENERIC_SUBSPACE;
  617. i < ACPI_PCCT_TYPE_RESERVED; i++) {
  618. proc[i].id = i;
  619. proc[i].count = 0;
  620. proc[i].handler = parse_pcc_subspace;
  621. }
  622. count = acpi_table_parse_entries_array(ACPI_SIG_PCCT,
  623. sizeof(struct acpi_table_pcct), proc,
  624. ACPI_PCCT_TYPE_RESERVED, MAX_PCC_SUBSPACES);
  625. if (count <= 0 || count > MAX_PCC_SUBSPACES) {
  626. if (count < 0)
  627. pr_warn("Error parsing PCC subspaces from PCCT\n");
  628. else
  629. pr_warn("Invalid PCCT: %d PCC subspaces\n", count);
  630. rc = -EINVAL;
  631. } else {
  632. pcc_chan_count = count;
  633. }
  634. acpi_put_table(pcct_tbl);
  635. return rc;
  636. }
  637. /**
  638. * pcc_mbox_probe - Called when we find a match for the
  639. * PCCT platform device. This is purely used to represent
  640. * the PCCT as a virtual device for registering with the
  641. * generic Mailbox framework.
  642. *
  643. * @pdev: Pointer to platform device returned when a match
  644. * is found.
  645. *
  646. * Return: 0 for Success, else errno.
  647. */
  648. static int pcc_mbox_probe(struct platform_device *pdev)
  649. {
  650. struct device *dev = &pdev->dev;
  651. struct mbox_controller *pcc_mbox_ctrl;
  652. struct mbox_chan *pcc_mbox_channels;
  653. struct acpi_table_header *pcct_tbl;
  654. struct acpi_subtable_header *pcct_entry;
  655. struct acpi_table_pcct *acpi_pcct_tbl;
  656. acpi_status status = AE_OK;
  657. int i, rc, count = pcc_chan_count;
  658. /* Search for PCCT */
  659. status = acpi_get_table(ACPI_SIG_PCCT, 0, &pcct_tbl);
  660. if (ACPI_FAILURE(status) || !pcct_tbl)
  661. return -ENODEV;
  662. pcc_mbox_channels = devm_kcalloc(dev, count, sizeof(*pcc_mbox_channels),
  663. GFP_KERNEL);
  664. if (!pcc_mbox_channels) {
  665. rc = -ENOMEM;
  666. goto err;
  667. }
  668. chan_info = devm_kcalloc(dev, count, sizeof(*chan_info), GFP_KERNEL);
  669. if (!chan_info) {
  670. rc = -ENOMEM;
  671. goto err;
  672. }
  673. pcc_mbox_ctrl = devm_kzalloc(dev, sizeof(*pcc_mbox_ctrl), GFP_KERNEL);
  674. if (!pcc_mbox_ctrl) {
  675. rc = -ENOMEM;
  676. goto err;
  677. }
  678. /* Point to the first PCC subspace entry */
  679. pcct_entry = (struct acpi_subtable_header *) (
  680. (unsigned long) pcct_tbl + sizeof(struct acpi_table_pcct));
  681. acpi_pcct_tbl = (struct acpi_table_pcct *) pcct_tbl;
  682. if (acpi_pcct_tbl->flags & ACPI_PCCT_DOORBELL)
  683. pcc_mbox_ctrl->txdone_irq = true;
  684. for (i = 0; i < count; i++) {
  685. struct pcc_chan_info *pchan = chan_info + i;
  686. pcc_mbox_channels[i].con_priv = pchan;
  687. pchan->chan.mchan = &pcc_mbox_channels[i];
  688. if (pcct_entry->type == ACPI_PCCT_TYPE_EXT_PCC_SLAVE_SUBSPACE &&
  689. !pcc_mbox_ctrl->txdone_irq) {
  690. pr_err("Platform Interrupt flag must be set to 1");
  691. rc = -EINVAL;
  692. goto err;
  693. }
  694. if (pcc_mbox_ctrl->txdone_irq) {
  695. rc = pcc_parse_subspace_irq(pchan, pcct_entry);
  696. if (rc < 0)
  697. goto err;
  698. }
  699. rc = pcc_parse_subspace_db_reg(pchan, pcct_entry);
  700. if (rc < 0)
  701. goto err;
  702. pcc_parse_subspace_shmem(pchan, pcct_entry);
  703. pchan->type = pcct_entry->type;
  704. pcct_entry = (struct acpi_subtable_header *)
  705. ((unsigned long) pcct_entry + pcct_entry->length);
  706. }
  707. pcc_mbox_ctrl->num_chans = count;
  708. pr_info("Detected %d PCC Subspaces\n", pcc_mbox_ctrl->num_chans);
  709. pcc_mbox_ctrl->chans = pcc_mbox_channels;
  710. pcc_mbox_ctrl->ops = &pcc_chan_ops;
  711. pcc_mbox_ctrl->dev = dev;
  712. pr_info("Registering PCC driver as Mailbox controller\n");
  713. rc = mbox_controller_register(pcc_mbox_ctrl);
  714. if (rc)
  715. pr_err("Err registering PCC as Mailbox controller: %d\n", rc);
  716. else
  717. return 0;
  718. err:
  719. acpi_put_table(pcct_tbl);
  720. return rc;
  721. }
  722. static struct platform_driver pcc_mbox_driver = {
  723. .probe = pcc_mbox_probe,
  724. .driver = {
  725. .name = "PCCT",
  726. },
  727. };
  728. static int __init pcc_init(void)
  729. {
  730. int ret;
  731. struct platform_device *pcc_pdev;
  732. if (acpi_disabled)
  733. return -ENODEV;
  734. /* Check if PCC support is available. */
  735. ret = acpi_pcc_probe();
  736. if (ret) {
  737. pr_debug("ACPI PCC probe failed.\n");
  738. return -ENODEV;
  739. }
  740. pcc_pdev = platform_create_bundle(&pcc_mbox_driver,
  741. pcc_mbox_probe, NULL, 0, NULL, 0);
  742. if (IS_ERR(pcc_pdev)) {
  743. pr_debug("Err creating PCC platform bundle\n");
  744. pcc_chan_count = 0;
  745. return PTR_ERR(pcc_pdev);
  746. }
  747. return 0;
  748. }
  749. /*
  750. * Make PCC init postcore so that users of this mailbox
  751. * such as the ACPI Processor driver have it available
  752. * at their init.
  753. */
  754. postcore_initcall(pcc_init);