pcc.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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.
  24. *
  25. * Typical high level flow of operation is:
  26. *
  27. * PCC Reads:
  28. * * Client tries to acquire a channel lock.
  29. * * After it is acquired it writes READ cmd in communication region cmd
  30. * address.
  31. * * Client issues mbox_send_message() which rings the PCC doorbell
  32. * for its PCC channel.
  33. * * If command completes, then client has control over channel and
  34. * it can proceed with its reads.
  35. * * Client releases lock.
  36. *
  37. * PCC Writes:
  38. * * Client tries to acquire channel lock.
  39. * * Client writes to its communication region after it acquires a
  40. * channel lock.
  41. * * Client writes WRITE cmd in communication region cmd address.
  42. * * Client issues mbox_send_message() which rings the PCC doorbell
  43. * for its PCC channel.
  44. * * If command completes, then writes have succeded and it can release
  45. * the channel lock.
  46. *
  47. * There is a Nominal latency defined for each channel which indicates
  48. * how long to wait until a command completes. If command is not complete
  49. * the client needs to retry or assume failure.
  50. *
  51. * For more details about PCC, please see the ACPI specification from
  52. * http://www.uefi.org/ACPIv5.1 Section 14.
  53. *
  54. * This file implements PCC as a Mailbox controller and allows for PCC
  55. * clients to be implemented as its Mailbox Client Channels.
  56. */
  57. #include <linux/acpi.h>
  58. #include <linux/delay.h>
  59. #include <linux/io.h>
  60. #include <linux/init.h>
  61. #include <linux/list.h>
  62. #include <linux/platform_device.h>
  63. #include <linux/mailbox_controller.h>
  64. #include <linux/mailbox_client.h>
  65. #include "mailbox.h"
  66. #define MAX_PCC_SUBSPACES 256
  67. static struct mbox_chan *pcc_mbox_channels;
  68. static struct mbox_controller pcc_mbox_ctrl = {};
  69. /**
  70. * get_pcc_channel - Given a PCC subspace idx, get
  71. * the respective mbox_channel.
  72. * @id: PCC subspace index.
  73. *
  74. * Return: ERR_PTR(errno) if error, else pointer
  75. * to mbox channel.
  76. */
  77. static struct mbox_chan *get_pcc_channel(int id)
  78. {
  79. struct mbox_chan *pcc_chan;
  80. if (id < 0 || id > pcc_mbox_ctrl.num_chans)
  81. return ERR_PTR(-ENOENT);
  82. pcc_chan = (struct mbox_chan *)
  83. (unsigned long) pcc_mbox_channels +
  84. (id * sizeof(*pcc_chan));
  85. return pcc_chan;
  86. }
  87. /**
  88. * pcc_mbox_request_channel - PCC clients call this function to
  89. * request a pointer to their PCC subspace, from which they
  90. * can get the details of communicating with the remote.
  91. * @cl: Pointer to Mailbox client, so we know where to bind the
  92. * Channel.
  93. * @subspace_id: The PCC Subspace index as parsed in the PCC client
  94. * ACPI package. This is used to lookup the array of PCC
  95. * subspaces as parsed by the PCC Mailbox controller.
  96. *
  97. * Return: Pointer to the Mailbox Channel if successful or
  98. * ERR_PTR.
  99. */
  100. struct mbox_chan *pcc_mbox_request_channel(struct mbox_client *cl,
  101. int subspace_id)
  102. {
  103. struct device *dev = pcc_mbox_ctrl.dev;
  104. struct mbox_chan *chan;
  105. unsigned long flags;
  106. /*
  107. * Each PCC Subspace is a Mailbox Channel.
  108. * The PCC Clients get their PCC Subspace ID
  109. * from their own tables and pass it here.
  110. * This returns a pointer to the PCC subspace
  111. * for the Client to operate on.
  112. */
  113. chan = get_pcc_channel(subspace_id);
  114. if (IS_ERR(chan) || chan->cl) {
  115. dev_err(dev, "Channel not found for idx: %d\n", subspace_id);
  116. return ERR_PTR(-EBUSY);
  117. }
  118. spin_lock_irqsave(&chan->lock, flags);
  119. chan->msg_free = 0;
  120. chan->msg_count = 0;
  121. chan->active_req = NULL;
  122. chan->cl = cl;
  123. init_completion(&chan->tx_complete);
  124. if (chan->txdone_method == TXDONE_BY_POLL && cl->knows_txdone)
  125. chan->txdone_method |= TXDONE_BY_ACK;
  126. spin_unlock_irqrestore(&chan->lock, flags);
  127. return chan;
  128. }
  129. EXPORT_SYMBOL_GPL(pcc_mbox_request_channel);
  130. /**
  131. * pcc_mbox_free_channel - Clients call this to free their Channel.
  132. *
  133. * @chan: Pointer to the mailbox channel as returned by
  134. * pcc_mbox_request_channel()
  135. */
  136. void pcc_mbox_free_channel(struct mbox_chan *chan)
  137. {
  138. unsigned long flags;
  139. if (!chan || !chan->cl)
  140. return;
  141. spin_lock_irqsave(&chan->lock, flags);
  142. chan->cl = NULL;
  143. chan->active_req = NULL;
  144. if (chan->txdone_method == (TXDONE_BY_POLL | TXDONE_BY_ACK))
  145. chan->txdone_method = TXDONE_BY_POLL;
  146. spin_unlock_irqrestore(&chan->lock, flags);
  147. }
  148. EXPORT_SYMBOL_GPL(pcc_mbox_free_channel);
  149. /**
  150. * pcc_send_data - Called from Mailbox Controller code. Used
  151. * here only to ring the channel doorbell. The PCC client
  152. * specific read/write is done in the client driver in
  153. * order to maintain atomicity over PCC channel once
  154. * OS has control over it. See above for flow of operations.
  155. * @chan: Pointer to Mailbox channel over which to send data.
  156. * @data: Client specific data written over channel. Used here
  157. * only for debug after PCC transaction completes.
  158. *
  159. * Return: Err if something failed else 0 for success.
  160. */
  161. static int pcc_send_data(struct mbox_chan *chan, void *data)
  162. {
  163. struct acpi_pcct_hw_reduced *pcct_ss = chan->con_priv;
  164. struct acpi_generic_address doorbell;
  165. u64 doorbell_preserve;
  166. u64 doorbell_val;
  167. u64 doorbell_write;
  168. doorbell = pcct_ss->doorbell_register;
  169. doorbell_preserve = pcct_ss->preserve_mask;
  170. doorbell_write = pcct_ss->write_mask;
  171. /* Sync notification from OS to Platform. */
  172. acpi_read(&doorbell_val, &doorbell);
  173. acpi_write((doorbell_val & doorbell_preserve) | doorbell_write,
  174. &doorbell);
  175. return 0;
  176. }
  177. static const struct mbox_chan_ops pcc_chan_ops = {
  178. .send_data = pcc_send_data,
  179. };
  180. /**
  181. * parse_pcc_subspace - Parse the PCC table and verify PCC subspace
  182. * entries. There should be one entry per PCC client.
  183. * @header: Pointer to the ACPI subtable header under the PCCT.
  184. * @end: End of subtable entry.
  185. *
  186. * Return: 0 for Success, else errno.
  187. *
  188. * This gets called for each entry in the PCC table.
  189. */
  190. static int parse_pcc_subspace(struct acpi_subtable_header *header,
  191. const unsigned long end)
  192. {
  193. struct acpi_pcct_hw_reduced *pcct_ss;
  194. if (pcc_mbox_ctrl.num_chans <= MAX_PCC_SUBSPACES) {
  195. pcct_ss = (struct acpi_pcct_hw_reduced *) header;
  196. if (pcct_ss->header.type !=
  197. ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE) {
  198. pr_err("Incorrect PCC Subspace type detected\n");
  199. return -EINVAL;
  200. }
  201. }
  202. return 0;
  203. }
  204. /**
  205. * acpi_pcc_probe - Parse the ACPI tree for the PCCT.
  206. *
  207. * Return: 0 for Success, else errno.
  208. */
  209. static int __init acpi_pcc_probe(void)
  210. {
  211. acpi_size pcct_tbl_header_size;
  212. struct acpi_table_header *pcct_tbl;
  213. struct acpi_subtable_header *pcct_entry;
  214. int count, i;
  215. acpi_status status = AE_OK;
  216. /* Search for PCCT */
  217. status = acpi_get_table_with_size(ACPI_SIG_PCCT, 0,
  218. &pcct_tbl,
  219. &pcct_tbl_header_size);
  220. if (ACPI_FAILURE(status) || !pcct_tbl) {
  221. pr_warn("PCCT header not found.\n");
  222. return -ENODEV;
  223. }
  224. count = acpi_table_parse_entries(ACPI_SIG_PCCT,
  225. sizeof(struct acpi_table_pcct),
  226. ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE,
  227. parse_pcc_subspace, MAX_PCC_SUBSPACES);
  228. if (count <= 0) {
  229. pr_err("Error parsing PCC subspaces from PCCT\n");
  230. return -EINVAL;
  231. }
  232. pcc_mbox_channels = kzalloc(sizeof(struct mbox_chan) *
  233. count, GFP_KERNEL);
  234. if (!pcc_mbox_channels) {
  235. pr_err("Could not allocate space for PCC mbox channels\n");
  236. return -ENOMEM;
  237. }
  238. /* Point to the first PCC subspace entry */
  239. pcct_entry = (struct acpi_subtable_header *) (
  240. (unsigned long) pcct_tbl + sizeof(struct acpi_table_pcct));
  241. for (i = 0; i < count; i++) {
  242. pcc_mbox_channels[i].con_priv = pcct_entry;
  243. pcct_entry = (struct acpi_subtable_header *)
  244. ((unsigned long) pcct_entry + pcct_entry->length);
  245. }
  246. pcc_mbox_ctrl.num_chans = count;
  247. pr_info("Detected %d PCC Subspaces\n", pcc_mbox_ctrl.num_chans);
  248. return 0;
  249. }
  250. /**
  251. * pcc_mbox_probe - Called when we find a match for the
  252. * PCCT platform device. This is purely used to represent
  253. * the PCCT as a virtual device for registering with the
  254. * generic Mailbox framework.
  255. *
  256. * @pdev: Pointer to platform device returned when a match
  257. * is found.
  258. *
  259. * Return: 0 for Success, else errno.
  260. */
  261. static int pcc_mbox_probe(struct platform_device *pdev)
  262. {
  263. int ret = 0;
  264. pcc_mbox_ctrl.chans = pcc_mbox_channels;
  265. pcc_mbox_ctrl.ops = &pcc_chan_ops;
  266. pcc_mbox_ctrl.dev = &pdev->dev;
  267. pr_info("Registering PCC driver as Mailbox controller\n");
  268. ret = mbox_controller_register(&pcc_mbox_ctrl);
  269. if (ret) {
  270. pr_err("Err registering PCC as Mailbox controller: %d\n", ret);
  271. ret = -ENODEV;
  272. }
  273. return ret;
  274. }
  275. struct platform_driver pcc_mbox_driver = {
  276. .probe = pcc_mbox_probe,
  277. .driver = {
  278. .name = "PCCT",
  279. .owner = THIS_MODULE,
  280. },
  281. };
  282. static int __init pcc_init(void)
  283. {
  284. int ret;
  285. struct platform_device *pcc_pdev;
  286. if (acpi_disabled)
  287. return -ENODEV;
  288. /* Check if PCC support is available. */
  289. ret = acpi_pcc_probe();
  290. if (ret) {
  291. pr_debug("ACPI PCC probe failed.\n");
  292. return -ENODEV;
  293. }
  294. pcc_pdev = platform_create_bundle(&pcc_mbox_driver,
  295. pcc_mbox_probe, NULL, 0, NULL, 0);
  296. if (IS_ERR(pcc_pdev)) {
  297. pr_debug("Err creating PCC platform bundle\n");
  298. return PTR_ERR(pcc_pdev);
  299. }
  300. return 0;
  301. }
  302. /*
  303. * Make PCC init postcore so that users of this mailbox
  304. * such as the ACPI Processor driver have it available
  305. * at their init.
  306. */
  307. postcore_initcall(pcc_init);