pcc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * Copyright (C) 2014 Linaro Ltd.
  3. * Author: Ashwin Chaugule <ashwin.chaugule@linaro.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * PCC (Platform Communication Channel) is defined in the ACPI 5.0+
  16. * specification. It is a mailbox like mechanism to allow clients
  17. * such as CPPC (Collaborative Processor Performance Control), RAS
  18. * (Reliability, Availability and Serviceability) and MPST (Memory
  19. * Node Power State Table) to talk to the platform (e.g. BMC) through
  20. * shared memory regions as defined in the PCC table entries. The PCC
  21. * specification supports a Doorbell mechanism for the PCC clients
  22. * to notify the platform about new data. This Doorbell information
  23. * is also specified in each PCC table entry. See pcc_send_data()
  24. * and pcc_tx_done() for basic mode of operation.
  25. *
  26. * For more details about PCC, please see the ACPI specification from
  27. * http://www.uefi.org/ACPIv5.1 Section 14.
  28. *
  29. * This file implements PCC as a Mailbox controller and allows for PCC
  30. * clients to be implemented as its Mailbox Client Channels.
  31. */
  32. #include <linux/acpi.h>
  33. #include <linux/delay.h>
  34. #include <linux/io.h>
  35. #include <linux/init.h>
  36. #include <linux/list.h>
  37. #include <linux/platform_device.h>
  38. #include <linux/mailbox_controller.h>
  39. #include <linux/mailbox_client.h>
  40. #include "mailbox.h"
  41. #define MAX_PCC_SUBSPACES 256
  42. #define PCCS_SS_SIG_MAGIC 0x50434300
  43. #define PCC_CMD_COMPLETE 0x1
  44. static struct mbox_chan *pcc_mbox_channels;
  45. static struct mbox_controller pcc_mbox_ctrl = {};
  46. /**
  47. * get_pcc_channel - Given a PCC subspace idx, get
  48. * the respective mbox_channel.
  49. * @id: PCC subspace index.
  50. *
  51. * Return: ERR_PTR(errno) if error, else pointer
  52. * to mbox channel.
  53. */
  54. static struct mbox_chan *get_pcc_channel(int id)
  55. {
  56. struct mbox_chan *pcc_chan;
  57. if (id < 0 || id > pcc_mbox_ctrl.num_chans)
  58. return ERR_PTR(-ENOENT);
  59. pcc_chan = (struct mbox_chan *)
  60. (unsigned long) pcc_mbox_channels +
  61. (id * sizeof(*pcc_chan));
  62. return pcc_chan;
  63. }
  64. /**
  65. * get_subspace_id - Given a Mailbox channel, find out the
  66. * PCC subspace id.
  67. * @chan: Pointer to Mailbox Channel from which we want
  68. * the index.
  69. * Return: Errno if not found, else positive index number.
  70. */
  71. static int get_subspace_id(struct mbox_chan *chan)
  72. {
  73. unsigned int id = chan - pcc_mbox_channels;
  74. if (id < 0 || id > pcc_mbox_ctrl.num_chans)
  75. return -ENOENT;
  76. return id;
  77. }
  78. /**
  79. * pcc_mbox_request_channel - PCC clients call this function to
  80. * request a pointer to their PCC subspace, from which they
  81. * can get the details of communicating with the remote.
  82. * @cl: Pointer to Mailbox client, so we know where to bind the
  83. * Channel.
  84. * @subspace_id: The PCC Subspace index as parsed in the PCC client
  85. * ACPI package. This is used to lookup the array of PCC
  86. * subspaces as parsed by the PCC Mailbox controller.
  87. *
  88. * Return: Pointer to the Mailbox Channel if successful or
  89. * ERR_PTR.
  90. */
  91. struct mbox_chan *pcc_mbox_request_channel(struct mbox_client *cl,
  92. int subspace_id)
  93. {
  94. struct device *dev = pcc_mbox_ctrl.dev;
  95. struct mbox_chan *chan;
  96. unsigned long flags;
  97. /*
  98. * Each PCC Subspace is a Mailbox Channel.
  99. * The PCC Clients get their PCC Subspace ID
  100. * from their own tables and pass it here.
  101. * This returns a pointer to the PCC subspace
  102. * for the Client to operate on.
  103. */
  104. chan = get_pcc_channel(subspace_id);
  105. if (!chan || chan->cl) {
  106. dev_err(dev, "%s: PCC mailbox not free\n", __func__);
  107. return ERR_PTR(-EBUSY);
  108. }
  109. spin_lock_irqsave(&chan->lock, flags);
  110. chan->msg_free = 0;
  111. chan->msg_count = 0;
  112. chan->active_req = NULL;
  113. chan->cl = cl;
  114. init_completion(&chan->tx_complete);
  115. if (chan->txdone_method == TXDONE_BY_POLL && cl->knows_txdone)
  116. chan->txdone_method |= TXDONE_BY_ACK;
  117. spin_unlock_irqrestore(&chan->lock, flags);
  118. return chan;
  119. }
  120. EXPORT_SYMBOL_GPL(pcc_mbox_request_channel);
  121. /**
  122. * pcc_mbox_free_channel - Clients call this to free their Channel.
  123. *
  124. * @chan: Pointer to the mailbox channel as returned by
  125. * pcc_mbox_request_channel()
  126. */
  127. void pcc_mbox_free_channel(struct mbox_chan *chan)
  128. {
  129. unsigned long flags;
  130. if (!chan || !chan->cl)
  131. return;
  132. spin_lock_irqsave(&chan->lock, flags);
  133. chan->cl = NULL;
  134. chan->active_req = NULL;
  135. if (chan->txdone_method == (TXDONE_BY_POLL | TXDONE_BY_ACK))
  136. chan->txdone_method = TXDONE_BY_POLL;
  137. spin_unlock_irqrestore(&chan->lock, flags);
  138. }
  139. EXPORT_SYMBOL_GPL(pcc_mbox_free_channel);
  140. /**
  141. * pcc_tx_done - Callback from Mailbox controller code to
  142. * check if PCC message transmission completed.
  143. * @chan: Pointer to Mailbox channel on which previous
  144. * transmission occurred.
  145. *
  146. * Return: TRUE if succeeded.
  147. */
  148. static bool pcc_tx_done(struct mbox_chan *chan)
  149. {
  150. struct acpi_pcct_hw_reduced *pcct_ss = chan->con_priv;
  151. struct acpi_pcct_shared_memory *generic_comm_base =
  152. (struct acpi_pcct_shared_memory *) pcct_ss->base_address;
  153. u16 cmd_delay = pcct_ss->latency;
  154. unsigned int retries = 0;
  155. /* Try a few times while waiting for platform to consume */
  156. while (!(readw_relaxed(&generic_comm_base->status)
  157. & PCC_CMD_COMPLETE)) {
  158. if (retries++ < 5)
  159. udelay(cmd_delay);
  160. else {
  161. /*
  162. * If the remote is dead, this will cause the Mbox
  163. * controller to timeout after mbox client.tx_tout
  164. * msecs.
  165. */
  166. pr_err("PCC platform did not respond.\n");
  167. return false;
  168. }
  169. }
  170. return true;
  171. }
  172. /**
  173. * pcc_send_data - Called from Mailbox Controller code to finally
  174. * transmit data over channel.
  175. * @chan: Pointer to Mailbox channel over which to send data.
  176. * @data: Actual data to be written over channel.
  177. *
  178. * Return: Err if something failed else 0 for success.
  179. */
  180. static int pcc_send_data(struct mbox_chan *chan, void *data)
  181. {
  182. struct acpi_pcct_hw_reduced *pcct_ss = chan->con_priv;
  183. struct acpi_pcct_shared_memory *generic_comm_base =
  184. (struct acpi_pcct_shared_memory *) pcct_ss->base_address;
  185. struct acpi_generic_address doorbell;
  186. u64 doorbell_preserve;
  187. u64 doorbell_val;
  188. u64 doorbell_write;
  189. u16 cmd = *(u16 *) data;
  190. u16 ss_idx = -1;
  191. ss_idx = get_subspace_id(chan);
  192. if (ss_idx < 0) {
  193. pr_err("Invalid Subspace ID from PCC client\n");
  194. return -EINVAL;
  195. }
  196. doorbell = pcct_ss->doorbell_register;
  197. doorbell_preserve = pcct_ss->preserve_mask;
  198. doorbell_write = pcct_ss->write_mask;
  199. /* Write to the shared comm region. */
  200. writew(cmd, &generic_comm_base->command);
  201. /* Write Subspace MAGIC value so platform can identify destination. */
  202. writel((PCCS_SS_SIG_MAGIC | ss_idx), &generic_comm_base->signature);
  203. /* Flip CMD COMPLETE bit */
  204. writew(0, &generic_comm_base->status);
  205. /* Sync notification from OSPM to Platform. */
  206. acpi_read(&doorbell_val, &doorbell);
  207. acpi_write((doorbell_val & doorbell_preserve) | doorbell_write,
  208. &doorbell);
  209. return 0;
  210. }
  211. static struct mbox_chan_ops pcc_chan_ops = {
  212. .send_data = pcc_send_data,
  213. .last_tx_done = pcc_tx_done,
  214. };
  215. /**
  216. * parse_pcc_subspace - Parse the PCC table and verify PCC subspace
  217. * entries. There should be one entry per PCC client.
  218. * @header: Pointer to the ACPI subtable header under the PCCT.
  219. * @end: End of subtable entry.
  220. *
  221. * Return: 0 for Success, else errno.
  222. *
  223. * This gets called for each entry in the PCC table.
  224. */
  225. static int parse_pcc_subspace(struct acpi_subtable_header *header,
  226. const unsigned long end)
  227. {
  228. struct acpi_pcct_hw_reduced *pcct_ss;
  229. if (pcc_mbox_ctrl.num_chans <= MAX_PCC_SUBSPACES) {
  230. pcct_ss = (struct acpi_pcct_hw_reduced *) header;
  231. if (pcct_ss->header.type !=
  232. ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE) {
  233. pr_err("Incorrect PCC Subspace type detected\n");
  234. return -EINVAL;
  235. }
  236. }
  237. return 0;
  238. }
  239. /**
  240. * acpi_pcc_probe - Parse the ACPI tree for the PCCT.
  241. *
  242. * Return: 0 for Success, else errno.
  243. */
  244. static int __init acpi_pcc_probe(void)
  245. {
  246. acpi_size pcct_tbl_header_size;
  247. struct acpi_table_header *pcct_tbl;
  248. struct acpi_subtable_header *pcct_entry;
  249. int count, i;
  250. acpi_status status = AE_OK;
  251. /* Search for PCCT */
  252. status = acpi_get_table_with_size(ACPI_SIG_PCCT, 0,
  253. &pcct_tbl,
  254. &pcct_tbl_header_size);
  255. if (ACPI_FAILURE(status) || !pcct_tbl) {
  256. pr_warn("PCCT header not found.\n");
  257. return -ENODEV;
  258. }
  259. count = acpi_table_parse_entries(ACPI_SIG_PCCT,
  260. sizeof(struct acpi_table_pcct),
  261. ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE,
  262. parse_pcc_subspace, MAX_PCC_SUBSPACES);
  263. if (count <= 0) {
  264. pr_err("Error parsing PCC subspaces from PCCT\n");
  265. return -EINVAL;
  266. }
  267. pcc_mbox_channels = kzalloc(sizeof(struct mbox_chan) *
  268. count, GFP_KERNEL);
  269. if (!pcc_mbox_channels) {
  270. pr_err("Could not allocate space for PCC mbox channels\n");
  271. return -ENOMEM;
  272. }
  273. /* Point to the first PCC subspace entry */
  274. pcct_entry = (struct acpi_subtable_header *) (
  275. (unsigned long) pcct_tbl + sizeof(struct acpi_table_pcct));
  276. for (i = 0; i < count; i++) {
  277. pcc_mbox_channels[i].con_priv = pcct_entry;
  278. pcct_entry = (struct acpi_subtable_header *)
  279. ((unsigned long) pcct_entry + pcct_entry->length);
  280. }
  281. pcc_mbox_ctrl.num_chans = count;
  282. pr_info("Detected %d PCC Subspaces\n", pcc_mbox_ctrl.num_chans);
  283. return 0;
  284. }
  285. /**
  286. * pcc_mbox_probe - Called when we find a match for the
  287. * PCCT platform device. This is purely used to represent
  288. * the PCCT as a virtual device for registering with the
  289. * generic Mailbox framework.
  290. *
  291. * @pdev: Pointer to platform device returned when a match
  292. * is found.
  293. *
  294. * Return: 0 for Success, else errno.
  295. */
  296. static int pcc_mbox_probe(struct platform_device *pdev)
  297. {
  298. int ret = 0;
  299. pcc_mbox_ctrl.chans = pcc_mbox_channels;
  300. pcc_mbox_ctrl.ops = &pcc_chan_ops;
  301. pcc_mbox_ctrl.txdone_poll = true;
  302. pcc_mbox_ctrl.txpoll_period = 10;
  303. pcc_mbox_ctrl.dev = &pdev->dev;
  304. pr_info("Registering PCC driver as Mailbox controller\n");
  305. ret = mbox_controller_register(&pcc_mbox_ctrl);
  306. if (ret) {
  307. pr_err("Err registering PCC as Mailbox controller: %d\n", ret);
  308. ret = -ENODEV;
  309. }
  310. return ret;
  311. }
  312. struct platform_driver pcc_mbox_driver = {
  313. .probe = pcc_mbox_probe,
  314. .driver = {
  315. .name = "PCCT",
  316. .owner = THIS_MODULE,
  317. },
  318. };
  319. static int __init pcc_init(void)
  320. {
  321. int ret;
  322. struct platform_device *pcc_pdev;
  323. if (acpi_disabled)
  324. return -ENODEV;
  325. /* Check if PCC support is available. */
  326. ret = acpi_pcc_probe();
  327. if (ret) {
  328. pr_err("ACPI PCC probe failed.\n");
  329. return -ENODEV;
  330. }
  331. pcc_pdev = platform_create_bundle(&pcc_mbox_driver,
  332. pcc_mbox_probe, NULL, 0, NULL, 0);
  333. if (!pcc_pdev) {
  334. pr_err("Err creating PCC platform bundle\n");
  335. return -ENODEV;
  336. }
  337. return 0;
  338. }
  339. device_initcall(pcc_init);