pcc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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. /*
  114. * PCC can be used with perf critical drivers such as CPPC
  115. * So it makes sense to locally cache the virtual address and
  116. * use it to read/write to PCC registers such as doorbell register
  117. *
  118. * The below read_register and write_registers are used to read and
  119. * write from perf critical registers such as PCC doorbell register
  120. */
  121. static void read_register(void __iomem *vaddr, u64 *val, unsigned int bit_width)
  122. {
  123. switch (bit_width) {
  124. case 8:
  125. *val = readb(vaddr);
  126. break;
  127. case 16:
  128. *val = readw(vaddr);
  129. break;
  130. case 32:
  131. *val = readl(vaddr);
  132. break;
  133. case 64:
  134. *val = readq(vaddr);
  135. break;
  136. }
  137. }
  138. static void write_register(void __iomem *vaddr, u64 val, unsigned int bit_width)
  139. {
  140. switch (bit_width) {
  141. case 8:
  142. writeb(val, vaddr);
  143. break;
  144. case 16:
  145. writew(val, vaddr);
  146. break;
  147. case 32:
  148. writel(val, vaddr);
  149. break;
  150. case 64:
  151. writeq(val, vaddr);
  152. break;
  153. }
  154. }
  155. static int pcc_chan_reg_read(struct pcc_chan_reg *reg, u64 *val)
  156. {
  157. int ret = 0;
  158. if (!reg->gas) {
  159. *val = 0;
  160. return 0;
  161. }
  162. if (reg->vaddr)
  163. read_register(reg->vaddr, val, reg->gas->bit_width);
  164. else
  165. ret = acpi_read(val, reg->gas);
  166. return ret;
  167. }
  168. static int pcc_chan_reg_write(struct pcc_chan_reg *reg, u64 val)
  169. {
  170. int ret = 0;
  171. if (!reg->gas)
  172. return 0;
  173. if (reg->vaddr)
  174. write_register(reg->vaddr, val, reg->gas->bit_width);
  175. else
  176. ret = acpi_write(val, reg->gas);
  177. return ret;
  178. }
  179. static int pcc_chan_reg_read_modify_write(struct pcc_chan_reg *reg)
  180. {
  181. int ret = 0;
  182. u64 val;
  183. ret = pcc_chan_reg_read(reg, &val);
  184. if (ret)
  185. return ret;
  186. val &= reg->preserve_mask;
  187. val |= reg->set_mask;
  188. return pcc_chan_reg_write(reg, val);
  189. }
  190. /**
  191. * pcc_map_interrupt - Map a PCC subspace GSI to a linux IRQ number
  192. * @interrupt: GSI number.
  193. * @flags: interrupt flags
  194. *
  195. * Returns: a valid linux IRQ number on success
  196. * 0 or -EINVAL on failure
  197. */
  198. static int pcc_map_interrupt(u32 interrupt, u32 flags)
  199. {
  200. int trigger, polarity;
  201. if (!interrupt)
  202. return 0;
  203. trigger = (flags & ACPI_PCCT_INTERRUPT_MODE) ? ACPI_EDGE_SENSITIVE
  204. : ACPI_LEVEL_SENSITIVE;
  205. polarity = (flags & ACPI_PCCT_INTERRUPT_POLARITY) ? ACPI_ACTIVE_LOW
  206. : ACPI_ACTIVE_HIGH;
  207. return acpi_register_gsi(NULL, interrupt, trigger, polarity);
  208. }
  209. static bool pcc_chan_plat_irq_can_be_shared(struct pcc_chan_info *pchan)
  210. {
  211. return (pchan->plat_irq_flags & ACPI_PCCT_INTERRUPT_MODE) ==
  212. ACPI_LEVEL_SENSITIVE;
  213. }
  214. static bool pcc_mbox_cmd_complete_check(struct pcc_chan_info *pchan)
  215. {
  216. u64 val;
  217. int ret;
  218. if (!pchan->cmd_complete.gas)
  219. return true;
  220. ret = pcc_chan_reg_read(&pchan->cmd_complete, &val);
  221. if (ret)
  222. return false;
  223. /*
  224. * Judge if the channel respond the interrupt based on the value of
  225. * command complete.
  226. */
  227. val &= pchan->cmd_complete.status_mask;
  228. /*
  229. * If this is PCC slave subspace channel, and the command complete
  230. * bit 0 indicates that Platform is sending a notification and OSPM
  231. * needs to respond this interrupt to process this command.
  232. */
  233. if (pchan->type == ACPI_PCCT_TYPE_EXT_PCC_SLAVE_SUBSPACE)
  234. return !val;
  235. return !!val;
  236. }
  237. static int pcc_mbox_error_check_and_clear(struct pcc_chan_info *pchan)
  238. {
  239. u64 val;
  240. int ret;
  241. ret = pcc_chan_reg_read(&pchan->error, &val);
  242. if (ret)
  243. return ret;
  244. if (val & pchan->error.status_mask) {
  245. val &= pchan->error.preserve_mask;
  246. pcc_chan_reg_write(&pchan->error, val);
  247. return -EIO;
  248. }
  249. return 0;
  250. }
  251. static void pcc_chan_acknowledge(struct pcc_chan_info *pchan)
  252. {
  253. struct acpi_pcct_ext_pcc_shared_memory __iomem *pcc_hdr;
  254. if (pchan->type != ACPI_PCCT_TYPE_EXT_PCC_SLAVE_SUBSPACE)
  255. return;
  256. pcc_chan_reg_read_modify_write(&pchan->cmd_update);
  257. pcc_hdr = pchan->chan.shmem;
  258. /*
  259. * The PCC slave subspace channel needs to set the command
  260. * complete bit after processing message. If the PCC_ACK_FLAG
  261. * is set, it should also ring the doorbell.
  262. */
  263. if (ioread32(&pcc_hdr->flags) & PCC_CMD_COMPLETION_NOTIFY)
  264. pcc_chan_reg_read_modify_write(&pchan->db);
  265. }
  266. /**
  267. * pcc_mbox_irq - PCC mailbox interrupt handler
  268. * @irq: interrupt number
  269. * @p: data/cookie passed from the caller to identify the channel
  270. *
  271. * Returns: IRQ_HANDLED if interrupt is handled or IRQ_NONE if not
  272. */
  273. static irqreturn_t pcc_mbox_irq(int irq, void *p)
  274. {
  275. struct pcc_chan_info *pchan;
  276. struct mbox_chan *chan = p;
  277. pchan = chan->con_priv;
  278. if (pcc_chan_reg_read_modify_write(&pchan->plat_irq_ack))
  279. return IRQ_NONE;
  280. if (pchan->type == ACPI_PCCT_TYPE_EXT_PCC_MASTER_SUBSPACE &&
  281. !pchan->chan_in_use)
  282. return IRQ_NONE;
  283. if (!pcc_mbox_cmd_complete_check(pchan))
  284. return IRQ_NONE;
  285. if (pcc_mbox_error_check_and_clear(pchan))
  286. return IRQ_NONE;
  287. /*
  288. * Clear this flag after updating interrupt ack register and just
  289. * before mbox_chan_received_data() which might call pcc_send_data()
  290. * where the flag is set again to start new transfer. This is
  291. * required to avoid any possible race in updatation of this flag.
  292. */
  293. pchan->chan_in_use = false;
  294. mbox_chan_received_data(chan, NULL);
  295. mbox_chan_txdone(chan, 0);
  296. pcc_chan_acknowledge(pchan);
  297. return IRQ_HANDLED;
  298. }
  299. /**
  300. * pcc_mbox_request_channel - PCC clients call this function to
  301. * request a pointer to their PCC subspace, from which they
  302. * can get the details of communicating with the remote.
  303. * @cl: Pointer to Mailbox client, so we know where to bind the
  304. * Channel.
  305. * @subspace_id: The PCC Subspace index as parsed in the PCC client
  306. * ACPI package. This is used to lookup the array of PCC
  307. * subspaces as parsed by the PCC Mailbox controller.
  308. *
  309. * Return: Pointer to the PCC Mailbox Channel if successful or ERR_PTR.
  310. */
  311. struct pcc_mbox_chan *
  312. pcc_mbox_request_channel(struct mbox_client *cl, int subspace_id)
  313. {
  314. struct pcc_mbox_chan *pcc_mchan;
  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. pcc_mchan = &pchan->chan;
  327. pcc_mchan->shmem = acpi_os_ioremap(pcc_mchan->shmem_base_addr,
  328. pcc_mchan->shmem_size);
  329. if (!pcc_mchan->shmem)
  330. return ERR_PTR(-ENXIO);
  331. rc = mbox_bind_client(chan, cl);
  332. if (rc) {
  333. iounmap(pcc_mchan->shmem);
  334. pcc_mchan->shmem = NULL;
  335. return ERR_PTR(rc);
  336. }
  337. return pcc_mchan;
  338. }
  339. EXPORT_SYMBOL_GPL(pcc_mbox_request_channel);
  340. /**
  341. * pcc_mbox_free_channel - Clients call this to free their Channel.
  342. *
  343. * @pchan: Pointer to the PCC mailbox channel as returned by
  344. * pcc_mbox_request_channel()
  345. */
  346. void pcc_mbox_free_channel(struct pcc_mbox_chan *pchan)
  347. {
  348. struct mbox_chan *chan = pchan->mchan;
  349. struct pcc_chan_info *pchan_info;
  350. struct pcc_mbox_chan *pcc_mbox_chan;
  351. if (!chan || !chan->cl)
  352. return;
  353. pchan_info = chan->con_priv;
  354. pcc_mbox_chan = &pchan_info->chan;
  355. if (pcc_mbox_chan->shmem) {
  356. iounmap(pcc_mbox_chan->shmem);
  357. pcc_mbox_chan->shmem = NULL;
  358. }
  359. mbox_free_channel(chan);
  360. }
  361. EXPORT_SYMBOL_GPL(pcc_mbox_free_channel);
  362. /**
  363. * pcc_send_data - Called from Mailbox Controller code. Used
  364. * here only to ring the channel doorbell. The PCC client
  365. * specific read/write is done in the client driver in
  366. * order to maintain atomicity over PCC channel once
  367. * OS has control over it. See above for flow of operations.
  368. * @chan: Pointer to Mailbox channel over which to send data.
  369. * @data: Client specific data written over channel. Used here
  370. * only for debug after PCC transaction completes.
  371. *
  372. * Return: Err if something failed else 0 for success.
  373. */
  374. static int pcc_send_data(struct mbox_chan *chan, void *data)
  375. {
  376. int ret;
  377. struct pcc_chan_info *pchan = chan->con_priv;
  378. ret = pcc_chan_reg_read_modify_write(&pchan->cmd_update);
  379. if (ret)
  380. return ret;
  381. ret = pcc_chan_reg_read_modify_write(&pchan->db);
  382. if (!ret && pchan->plat_irq > 0)
  383. pchan->chan_in_use = true;
  384. return ret;
  385. }
  386. static bool pcc_last_tx_done(struct mbox_chan *chan)
  387. {
  388. struct pcc_chan_info *pchan = chan->con_priv;
  389. return pcc_mbox_cmd_complete_check(pchan);
  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. /*
  404. * Clear and acknowledge any pending interrupts on responder channel
  405. * before enabling the interrupt
  406. */
  407. pcc_chan_acknowledge(pchan);
  408. if (pchan->plat_irq > 0) {
  409. irqflags = pcc_chan_plat_irq_can_be_shared(pchan) ?
  410. IRQF_SHARED : 0;
  411. rc = devm_request_irq(chan->mbox->dev, pchan->plat_irq, pcc_mbox_irq,
  412. irqflags, MBOX_IRQ_NAME, chan);
  413. if (unlikely(rc)) {
  414. dev_err(chan->mbox->dev, "failed to register PCC interrupt %d\n",
  415. pchan->plat_irq);
  416. return rc;
  417. }
  418. }
  419. return 0;
  420. }
  421. /**
  422. * pcc_shutdown - Called from Mailbox Controller code. Used here
  423. * to free the interrupt.
  424. * @chan: Pointer to Mailbox channel to shutdown.
  425. */
  426. static void pcc_shutdown(struct mbox_chan *chan)
  427. {
  428. struct pcc_chan_info *pchan = chan->con_priv;
  429. if (pchan->plat_irq > 0)
  430. devm_free_irq(chan->mbox->dev, pchan->plat_irq, chan);
  431. }
  432. static const struct mbox_chan_ops pcc_chan_ops = {
  433. .send_data = pcc_send_data,
  434. .startup = pcc_startup,
  435. .shutdown = pcc_shutdown,
  436. .last_tx_done = pcc_last_tx_done,
  437. };
  438. /**
  439. * parse_pcc_subspace - Count PCC subspaces defined
  440. * @header: Pointer to the ACPI subtable header under the PCCT.
  441. * @end: End of subtable entry.
  442. *
  443. * Return: If we find a PCC subspace entry of a valid type, return 0.
  444. * Otherwise, return -EINVAL.
  445. *
  446. * This gets called for each entry in the PCC table.
  447. */
  448. static int parse_pcc_subspace(union acpi_subtable_headers *header,
  449. const unsigned long end)
  450. {
  451. struct acpi_pcct_subspace *ss = (struct acpi_pcct_subspace *) header;
  452. if (ss->header.type < ACPI_PCCT_TYPE_RESERVED)
  453. return 0;
  454. return -EINVAL;
  455. }
  456. static int
  457. pcc_chan_reg_init(struct pcc_chan_reg *reg, struct acpi_generic_address *gas,
  458. u64 preserve_mask, u64 set_mask, u64 status_mask, char *name)
  459. {
  460. if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
  461. if (!(gas->bit_width >= 8 && gas->bit_width <= 64 &&
  462. is_power_of_2(gas->bit_width))) {
  463. pr_err("Error: Cannot access register of %u bit width",
  464. gas->bit_width);
  465. return -EFAULT;
  466. }
  467. reg->vaddr = acpi_os_ioremap(gas->address, gas->bit_width / 8);
  468. if (!reg->vaddr) {
  469. pr_err("Failed to ioremap PCC %s register\n", name);
  470. return -ENOMEM;
  471. }
  472. }
  473. reg->gas = gas;
  474. reg->preserve_mask = preserve_mask;
  475. reg->set_mask = set_mask;
  476. reg->status_mask = status_mask;
  477. return 0;
  478. }
  479. /**
  480. * pcc_parse_subspace_irq - Parse the PCC IRQ and PCC ACK register
  481. *
  482. * @pchan: Pointer to the PCC channel info structure.
  483. * @pcct_entry: Pointer to the ACPI subtable header.
  484. *
  485. * Return: 0 for Success, else errno.
  486. *
  487. * There should be one entry per PCC channel. This gets called for each
  488. * entry in the PCC table. This uses PCCY Type1 structure for all applicable
  489. * types(Type 1-4) to fetch irq
  490. */
  491. static int pcc_parse_subspace_irq(struct pcc_chan_info *pchan,
  492. struct acpi_subtable_header *pcct_entry)
  493. {
  494. int ret = 0;
  495. struct acpi_pcct_hw_reduced *pcct_ss;
  496. if (pcct_entry->type < ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE ||
  497. pcct_entry->type > ACPI_PCCT_TYPE_EXT_PCC_SLAVE_SUBSPACE)
  498. return 0;
  499. pcct_ss = (struct acpi_pcct_hw_reduced *)pcct_entry;
  500. pchan->plat_irq = pcc_map_interrupt(pcct_ss->platform_interrupt,
  501. (u32)pcct_ss->flags);
  502. if (pchan->plat_irq <= 0) {
  503. pr_err("PCC GSI %d not registered\n",
  504. pcct_ss->platform_interrupt);
  505. return -EINVAL;
  506. }
  507. pchan->plat_irq_flags = pcct_ss->flags;
  508. if (pcct_ss->header.type == ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE_TYPE2) {
  509. struct acpi_pcct_hw_reduced_type2 *pcct2_ss = (void *)pcct_ss;
  510. ret = pcc_chan_reg_init(&pchan->plat_irq_ack,
  511. &pcct2_ss->platform_ack_register,
  512. pcct2_ss->ack_preserve_mask,
  513. pcct2_ss->ack_write_mask, 0,
  514. "PLAT IRQ ACK");
  515. } else if (pcct_ss->header.type == ACPI_PCCT_TYPE_EXT_PCC_MASTER_SUBSPACE ||
  516. pcct_ss->header.type == ACPI_PCCT_TYPE_EXT_PCC_SLAVE_SUBSPACE) {
  517. struct acpi_pcct_ext_pcc_master *pcct_ext = (void *)pcct_ss;
  518. ret = pcc_chan_reg_init(&pchan->plat_irq_ack,
  519. &pcct_ext->platform_ack_register,
  520. pcct_ext->ack_preserve_mask,
  521. pcct_ext->ack_set_mask, 0,
  522. "PLAT IRQ ACK");
  523. }
  524. if (pcc_chan_plat_irq_can_be_shared(pchan) &&
  525. !pchan->plat_irq_ack.gas) {
  526. pr_err("PCC subspace has level IRQ with no ACK register\n");
  527. return -EINVAL;
  528. }
  529. return ret;
  530. }
  531. /**
  532. * pcc_parse_subspace_db_reg - Parse the PCC doorbell register
  533. *
  534. * @pchan: Pointer to the PCC channel info structure.
  535. * @pcct_entry: Pointer to the ACPI subtable header.
  536. *
  537. * Return: 0 for Success, else errno.
  538. */
  539. static int pcc_parse_subspace_db_reg(struct pcc_chan_info *pchan,
  540. struct acpi_subtable_header *pcct_entry)
  541. {
  542. int ret = 0;
  543. if (pcct_entry->type <= ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE_TYPE2) {
  544. struct acpi_pcct_subspace *pcct_ss;
  545. pcct_ss = (struct acpi_pcct_subspace *)pcct_entry;
  546. ret = pcc_chan_reg_init(&pchan->db,
  547. &pcct_ss->doorbell_register,
  548. pcct_ss->preserve_mask,
  549. pcct_ss->write_mask, 0, "Doorbell");
  550. } else {
  551. struct acpi_pcct_ext_pcc_master *pcct_ext;
  552. pcct_ext = (struct acpi_pcct_ext_pcc_master *)pcct_entry;
  553. ret = pcc_chan_reg_init(&pchan->db,
  554. &pcct_ext->doorbell_register,
  555. pcct_ext->preserve_mask,
  556. pcct_ext->write_mask, 0, "Doorbell");
  557. if (ret)
  558. return ret;
  559. ret = pcc_chan_reg_init(&pchan->cmd_complete,
  560. &pcct_ext->cmd_complete_register,
  561. 0, 0, pcct_ext->cmd_complete_mask,
  562. "Command Complete Check");
  563. if (ret)
  564. return ret;
  565. ret = pcc_chan_reg_init(&pchan->cmd_update,
  566. &pcct_ext->cmd_update_register,
  567. pcct_ext->cmd_update_preserve_mask,
  568. pcct_ext->cmd_update_set_mask, 0,
  569. "Command Complete Update");
  570. if (ret)
  571. return ret;
  572. ret = pcc_chan_reg_init(&pchan->error,
  573. &pcct_ext->error_status_register,
  574. ~pcct_ext->error_status_mask, 0,
  575. pcct_ext->error_status_mask,
  576. "Error Status");
  577. }
  578. return ret;
  579. }
  580. /**
  581. * pcc_parse_subspace_shmem - Parse the PCC Shared Memory Region information
  582. *
  583. * @pchan: Pointer to the PCC channel info structure.
  584. * @pcct_entry: Pointer to the ACPI subtable header.
  585. *
  586. */
  587. static void pcc_parse_subspace_shmem(struct pcc_chan_info *pchan,
  588. struct acpi_subtable_header *pcct_entry)
  589. {
  590. if (pcct_entry->type <= ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE_TYPE2) {
  591. struct acpi_pcct_subspace *pcct_ss =
  592. (struct acpi_pcct_subspace *)pcct_entry;
  593. pchan->chan.shmem_base_addr = pcct_ss->base_address;
  594. pchan->chan.shmem_size = pcct_ss->length;
  595. pchan->chan.latency = pcct_ss->latency;
  596. pchan->chan.max_access_rate = pcct_ss->max_access_rate;
  597. pchan->chan.min_turnaround_time = pcct_ss->min_turnaround_time;
  598. } else {
  599. struct acpi_pcct_ext_pcc_master *pcct_ext =
  600. (struct acpi_pcct_ext_pcc_master *)pcct_entry;
  601. pchan->chan.shmem_base_addr = pcct_ext->base_address;
  602. pchan->chan.shmem_size = pcct_ext->length;
  603. pchan->chan.latency = pcct_ext->latency;
  604. pchan->chan.max_access_rate = pcct_ext->max_access_rate;
  605. pchan->chan.min_turnaround_time = pcct_ext->min_turnaround_time;
  606. }
  607. }
  608. /**
  609. * acpi_pcc_probe - Parse the ACPI tree for the PCCT.
  610. *
  611. * Return: 0 for Success, else errno.
  612. */
  613. static int __init acpi_pcc_probe(void)
  614. {
  615. int count, i, rc = 0;
  616. acpi_status status;
  617. struct acpi_table_header *pcct_tbl;
  618. struct acpi_subtable_proc proc[ACPI_PCCT_TYPE_RESERVED];
  619. status = acpi_get_table(ACPI_SIG_PCCT, 0, &pcct_tbl);
  620. if (ACPI_FAILURE(status) || !pcct_tbl)
  621. return -ENODEV;
  622. /* Set up the subtable handlers */
  623. for (i = ACPI_PCCT_TYPE_GENERIC_SUBSPACE;
  624. i < ACPI_PCCT_TYPE_RESERVED; i++) {
  625. proc[i].id = i;
  626. proc[i].count = 0;
  627. proc[i].handler = parse_pcc_subspace;
  628. }
  629. count = acpi_table_parse_entries_array(ACPI_SIG_PCCT,
  630. sizeof(struct acpi_table_pcct), proc,
  631. ACPI_PCCT_TYPE_RESERVED, MAX_PCC_SUBSPACES);
  632. if (count <= 0 || count > MAX_PCC_SUBSPACES) {
  633. if (count < 0)
  634. pr_warn("Error parsing PCC subspaces from PCCT\n");
  635. else
  636. pr_warn("Invalid PCCT: %d PCC subspaces\n", count);
  637. rc = -EINVAL;
  638. } else {
  639. pcc_chan_count = count;
  640. }
  641. acpi_put_table(pcct_tbl);
  642. return rc;
  643. }
  644. /**
  645. * pcc_mbox_probe - Called when we find a match for the
  646. * PCCT platform device. This is purely used to represent
  647. * the PCCT as a virtual device for registering with the
  648. * generic Mailbox framework.
  649. *
  650. * @pdev: Pointer to platform device returned when a match
  651. * is found.
  652. *
  653. * Return: 0 for Success, else errno.
  654. */
  655. static int pcc_mbox_probe(struct platform_device *pdev)
  656. {
  657. struct device *dev = &pdev->dev;
  658. struct mbox_controller *pcc_mbox_ctrl;
  659. struct mbox_chan *pcc_mbox_channels;
  660. struct acpi_table_header *pcct_tbl;
  661. struct acpi_subtable_header *pcct_entry;
  662. struct acpi_table_pcct *acpi_pcct_tbl;
  663. acpi_status status = AE_OK;
  664. int i, rc, count = pcc_chan_count;
  665. /* Search for PCCT */
  666. status = acpi_get_table(ACPI_SIG_PCCT, 0, &pcct_tbl);
  667. if (ACPI_FAILURE(status) || !pcct_tbl)
  668. return -ENODEV;
  669. pcc_mbox_channels = devm_kcalloc(dev, count, sizeof(*pcc_mbox_channels),
  670. GFP_KERNEL);
  671. if (!pcc_mbox_channels) {
  672. rc = -ENOMEM;
  673. goto err;
  674. }
  675. chan_info = devm_kcalloc(dev, count, sizeof(*chan_info), GFP_KERNEL);
  676. if (!chan_info) {
  677. rc = -ENOMEM;
  678. goto err;
  679. }
  680. pcc_mbox_ctrl = devm_kzalloc(dev, sizeof(*pcc_mbox_ctrl), GFP_KERNEL);
  681. if (!pcc_mbox_ctrl) {
  682. rc = -ENOMEM;
  683. goto err;
  684. }
  685. /* Point to the first PCC subspace entry */
  686. pcct_entry = (struct acpi_subtable_header *) (
  687. (unsigned long) pcct_tbl + sizeof(struct acpi_table_pcct));
  688. acpi_pcct_tbl = (struct acpi_table_pcct *) pcct_tbl;
  689. if (acpi_pcct_tbl->flags & ACPI_PCCT_DOORBELL) {
  690. pcc_mbox_ctrl->txdone_irq = true;
  691. pcc_mbox_ctrl->txdone_poll = false;
  692. } else {
  693. pcc_mbox_ctrl->txdone_irq = false;
  694. pcc_mbox_ctrl->txdone_poll = true;
  695. }
  696. for (i = 0; i < count; i++) {
  697. struct pcc_chan_info *pchan = chan_info + i;
  698. pcc_mbox_channels[i].con_priv = pchan;
  699. pchan->chan.mchan = &pcc_mbox_channels[i];
  700. if (pcct_entry->type == ACPI_PCCT_TYPE_EXT_PCC_SLAVE_SUBSPACE &&
  701. !pcc_mbox_ctrl->txdone_irq) {
  702. pr_err("Platform Interrupt flag must be set to 1");
  703. rc = -EINVAL;
  704. goto err;
  705. }
  706. if (pcc_mbox_ctrl->txdone_irq) {
  707. rc = pcc_parse_subspace_irq(pchan, pcct_entry);
  708. if (rc < 0)
  709. goto err;
  710. }
  711. rc = pcc_parse_subspace_db_reg(pchan, pcct_entry);
  712. if (rc < 0)
  713. goto err;
  714. pcc_parse_subspace_shmem(pchan, pcct_entry);
  715. pchan->type = pcct_entry->type;
  716. pcct_entry = (struct acpi_subtable_header *)
  717. ((unsigned long) pcct_entry + pcct_entry->length);
  718. }
  719. pcc_mbox_ctrl->num_chans = count;
  720. pr_info("Detected %d PCC Subspaces\n", pcc_mbox_ctrl->num_chans);
  721. pcc_mbox_ctrl->chans = pcc_mbox_channels;
  722. pcc_mbox_ctrl->ops = &pcc_chan_ops;
  723. pcc_mbox_ctrl->dev = dev;
  724. pr_info("Registering PCC driver as Mailbox controller\n");
  725. rc = mbox_controller_register(pcc_mbox_ctrl);
  726. if (rc)
  727. pr_err("Err registering PCC as Mailbox controller: %d\n", rc);
  728. else
  729. return 0;
  730. err:
  731. acpi_put_table(pcct_tbl);
  732. return rc;
  733. }
  734. static struct platform_driver pcc_mbox_driver = {
  735. .probe = pcc_mbox_probe,
  736. .driver = {
  737. .name = "PCCT",
  738. },
  739. };
  740. static int __init pcc_init(void)
  741. {
  742. int ret;
  743. struct platform_device *pcc_pdev;
  744. if (acpi_disabled)
  745. return -ENODEV;
  746. /* Check if PCC support is available. */
  747. ret = acpi_pcc_probe();
  748. if (ret) {
  749. pr_debug("ACPI PCC probe failed.\n");
  750. return -ENODEV;
  751. }
  752. pcc_pdev = platform_create_bundle(&pcc_mbox_driver,
  753. pcc_mbox_probe, NULL, 0, NULL, 0);
  754. if (IS_ERR(pcc_pdev)) {
  755. pr_debug("Err creating PCC platform bundle\n");
  756. pcc_chan_count = 0;
  757. return PTR_ERR(pcc_pdev);
  758. }
  759. return 0;
  760. }
  761. /*
  762. * Make PCC init postcore so that users of this mailbox
  763. * such as the ACPI Processor driver have it available
  764. * at their init.
  765. */
  766. postcore_initcall(pcc_init);