pcc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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 <linux/io-64-nonatomic-lo-hi.h>
  66. #include "mailbox.h"
  67. #define MAX_PCC_SUBSPACES 256
  68. static struct mbox_chan *pcc_mbox_channels;
  69. /* Array of cached virtual address for doorbell registers */
  70. static void __iomem **pcc_doorbell_vaddr;
  71. static struct mbox_controller pcc_mbox_ctrl = {};
  72. /**
  73. * get_pcc_channel - Given a PCC subspace idx, get
  74. * the respective mbox_channel.
  75. * @id: PCC subspace index.
  76. *
  77. * Return: ERR_PTR(errno) if error, else pointer
  78. * to mbox channel.
  79. */
  80. static struct mbox_chan *get_pcc_channel(int id)
  81. {
  82. if (id < 0 || id > pcc_mbox_ctrl.num_chans)
  83. return ERR_PTR(-ENOENT);
  84. return &pcc_mbox_channels[id];
  85. }
  86. /**
  87. * pcc_mbox_request_channel - PCC clients call this function to
  88. * request a pointer to their PCC subspace, from which they
  89. * can get the details of communicating with the remote.
  90. * @cl: Pointer to Mailbox client, so we know where to bind the
  91. * Channel.
  92. * @subspace_id: The PCC Subspace index as parsed in the PCC client
  93. * ACPI package. This is used to lookup the array of PCC
  94. * subspaces as parsed by the PCC Mailbox controller.
  95. *
  96. * Return: Pointer to the Mailbox Channel if successful or
  97. * ERR_PTR.
  98. */
  99. struct mbox_chan *pcc_mbox_request_channel(struct mbox_client *cl,
  100. int subspace_id)
  101. {
  102. struct device *dev = pcc_mbox_ctrl.dev;
  103. struct mbox_chan *chan;
  104. unsigned long flags;
  105. /*
  106. * Each PCC Subspace is a Mailbox Channel.
  107. * The PCC Clients get their PCC Subspace ID
  108. * from their own tables and pass it here.
  109. * This returns a pointer to the PCC subspace
  110. * for the Client to operate on.
  111. */
  112. chan = get_pcc_channel(subspace_id);
  113. if (IS_ERR(chan) || chan->cl) {
  114. dev_err(dev, "Channel not found for idx: %d\n", subspace_id);
  115. return ERR_PTR(-EBUSY);
  116. }
  117. spin_lock_irqsave(&chan->lock, flags);
  118. chan->msg_free = 0;
  119. chan->msg_count = 0;
  120. chan->active_req = NULL;
  121. chan->cl = cl;
  122. init_completion(&chan->tx_complete);
  123. if (chan->txdone_method == TXDONE_BY_POLL && cl->knows_txdone)
  124. chan->txdone_method |= TXDONE_BY_ACK;
  125. spin_unlock_irqrestore(&chan->lock, flags);
  126. return chan;
  127. }
  128. EXPORT_SYMBOL_GPL(pcc_mbox_request_channel);
  129. /**
  130. * pcc_mbox_free_channel - Clients call this to free their Channel.
  131. *
  132. * @chan: Pointer to the mailbox channel as returned by
  133. * pcc_mbox_request_channel()
  134. */
  135. void pcc_mbox_free_channel(struct mbox_chan *chan)
  136. {
  137. unsigned long flags;
  138. if (!chan || !chan->cl)
  139. return;
  140. spin_lock_irqsave(&chan->lock, flags);
  141. chan->cl = NULL;
  142. chan->active_req = NULL;
  143. if (chan->txdone_method == (TXDONE_BY_POLL | TXDONE_BY_ACK))
  144. chan->txdone_method = TXDONE_BY_POLL;
  145. spin_unlock_irqrestore(&chan->lock, flags);
  146. }
  147. EXPORT_SYMBOL_GPL(pcc_mbox_free_channel);
  148. /*
  149. * PCC can be used with perf critical drivers such as CPPC
  150. * So it makes sense to locally cache the virtual address and
  151. * use it to read/write to PCC registers such as doorbell register
  152. *
  153. * The below read_register and write_registers are used to read and
  154. * write from perf critical registers such as PCC doorbell register
  155. */
  156. static int read_register(void __iomem *vaddr, u64 *val, unsigned int bit_width)
  157. {
  158. int ret_val = 0;
  159. switch (bit_width) {
  160. case 8:
  161. *val = readb(vaddr);
  162. break;
  163. case 16:
  164. *val = readw(vaddr);
  165. break;
  166. case 32:
  167. *val = readl(vaddr);
  168. break;
  169. case 64:
  170. *val = readq(vaddr);
  171. break;
  172. default:
  173. pr_debug("Error: Cannot read register of %u bit width",
  174. bit_width);
  175. ret_val = -EFAULT;
  176. break;
  177. }
  178. return ret_val;
  179. }
  180. static int write_register(void __iomem *vaddr, u64 val, unsigned int bit_width)
  181. {
  182. int ret_val = 0;
  183. switch (bit_width) {
  184. case 8:
  185. writeb(val, vaddr);
  186. break;
  187. case 16:
  188. writew(val, vaddr);
  189. break;
  190. case 32:
  191. writel(val, vaddr);
  192. break;
  193. case 64:
  194. writeq(val, vaddr);
  195. break;
  196. default:
  197. pr_debug("Error: Cannot write register of %u bit width",
  198. bit_width);
  199. ret_val = -EFAULT;
  200. break;
  201. }
  202. return ret_val;
  203. }
  204. /**
  205. * pcc_send_data - Called from Mailbox Controller code. Used
  206. * here only to ring the channel doorbell. The PCC client
  207. * specific read/write is done in the client driver in
  208. * order to maintain atomicity over PCC channel once
  209. * OS has control over it. See above for flow of operations.
  210. * @chan: Pointer to Mailbox channel over which to send data.
  211. * @data: Client specific data written over channel. Used here
  212. * only for debug after PCC transaction completes.
  213. *
  214. * Return: Err if something failed else 0 for success.
  215. */
  216. static int pcc_send_data(struct mbox_chan *chan, void *data)
  217. {
  218. struct acpi_pcct_hw_reduced *pcct_ss = chan->con_priv;
  219. struct acpi_generic_address *doorbell;
  220. u64 doorbell_preserve;
  221. u64 doorbell_val;
  222. u64 doorbell_write;
  223. u32 id = chan - pcc_mbox_channels;
  224. int ret = 0;
  225. if (id >= pcc_mbox_ctrl.num_chans) {
  226. pr_debug("pcc_send_data: Invalid mbox_chan passed\n");
  227. return -ENOENT;
  228. }
  229. doorbell = &pcct_ss->doorbell_register;
  230. doorbell_preserve = pcct_ss->preserve_mask;
  231. doorbell_write = pcct_ss->write_mask;
  232. /* Sync notification from OS to Platform. */
  233. if (pcc_doorbell_vaddr[id]) {
  234. ret = read_register(pcc_doorbell_vaddr[id], &doorbell_val,
  235. doorbell->bit_width);
  236. if (ret)
  237. return ret;
  238. ret = write_register(pcc_doorbell_vaddr[id],
  239. (doorbell_val & doorbell_preserve) | doorbell_write,
  240. doorbell->bit_width);
  241. } else {
  242. ret = acpi_read(&doorbell_val, doorbell);
  243. if (ret)
  244. return ret;
  245. ret = acpi_write((doorbell_val & doorbell_preserve) | doorbell_write,
  246. doorbell);
  247. }
  248. return ret;
  249. }
  250. static const struct mbox_chan_ops pcc_chan_ops = {
  251. .send_data = pcc_send_data,
  252. };
  253. /**
  254. * parse_pcc_subspace - Parse the PCC table and verify PCC subspace
  255. * entries. There should be one entry per PCC client.
  256. * @header: Pointer to the ACPI subtable header under the PCCT.
  257. * @end: End of subtable entry.
  258. *
  259. * Return: 0 for Success, else errno.
  260. *
  261. * This gets called for each entry in the PCC table.
  262. */
  263. static int parse_pcc_subspace(struct acpi_subtable_header *header,
  264. const unsigned long end)
  265. {
  266. struct acpi_pcct_hw_reduced *pcct_ss;
  267. if (pcc_mbox_ctrl.num_chans <= MAX_PCC_SUBSPACES) {
  268. pcct_ss = (struct acpi_pcct_hw_reduced *) header;
  269. if (pcct_ss->header.type !=
  270. ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE) {
  271. pr_err("Incorrect PCC Subspace type detected\n");
  272. return -EINVAL;
  273. }
  274. }
  275. return 0;
  276. }
  277. /**
  278. * acpi_pcc_probe - Parse the ACPI tree for the PCCT.
  279. *
  280. * Return: 0 for Success, else errno.
  281. */
  282. static int __init acpi_pcc_probe(void)
  283. {
  284. acpi_size pcct_tbl_header_size;
  285. struct acpi_table_header *pcct_tbl;
  286. struct acpi_subtable_header *pcct_entry;
  287. int count, i;
  288. acpi_status status = AE_OK;
  289. /* Search for PCCT */
  290. status = acpi_get_table_with_size(ACPI_SIG_PCCT, 0,
  291. &pcct_tbl,
  292. &pcct_tbl_header_size);
  293. if (ACPI_FAILURE(status) || !pcct_tbl) {
  294. pr_warn("PCCT header not found.\n");
  295. return -ENODEV;
  296. }
  297. count = acpi_table_parse_entries(ACPI_SIG_PCCT,
  298. sizeof(struct acpi_table_pcct),
  299. ACPI_PCCT_TYPE_HW_REDUCED_SUBSPACE,
  300. parse_pcc_subspace, MAX_PCC_SUBSPACES);
  301. if (count <= 0) {
  302. pr_err("Error parsing PCC subspaces from PCCT\n");
  303. return -EINVAL;
  304. }
  305. pcc_mbox_channels = kzalloc(sizeof(struct mbox_chan) *
  306. count, GFP_KERNEL);
  307. if (!pcc_mbox_channels) {
  308. pr_err("Could not allocate space for PCC mbox channels\n");
  309. return -ENOMEM;
  310. }
  311. pcc_doorbell_vaddr = kcalloc(count, sizeof(void *), GFP_KERNEL);
  312. if (!pcc_doorbell_vaddr) {
  313. kfree(pcc_mbox_channels);
  314. return -ENOMEM;
  315. }
  316. /* Point to the first PCC subspace entry */
  317. pcct_entry = (struct acpi_subtable_header *) (
  318. (unsigned long) pcct_tbl + sizeof(struct acpi_table_pcct));
  319. for (i = 0; i < count; i++) {
  320. struct acpi_generic_address *db_reg;
  321. struct acpi_pcct_hw_reduced *pcct_ss;
  322. pcc_mbox_channels[i].con_priv = pcct_entry;
  323. pcct_entry = (struct acpi_subtable_header *)
  324. ((unsigned long) pcct_entry + pcct_entry->length);
  325. /* If doorbell is in system memory cache the virt address */
  326. pcct_ss = (struct acpi_pcct_hw_reduced *)pcct_entry;
  327. db_reg = &pcct_ss->doorbell_register;
  328. if (db_reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
  329. pcc_doorbell_vaddr[i] = acpi_os_ioremap(db_reg->address,
  330. db_reg->bit_width/8);
  331. }
  332. pcc_mbox_ctrl.num_chans = count;
  333. pr_info("Detected %d PCC Subspaces\n", pcc_mbox_ctrl.num_chans);
  334. return 0;
  335. }
  336. /**
  337. * pcc_mbox_probe - Called when we find a match for the
  338. * PCCT platform device. This is purely used to represent
  339. * the PCCT as a virtual device for registering with the
  340. * generic Mailbox framework.
  341. *
  342. * @pdev: Pointer to platform device returned when a match
  343. * is found.
  344. *
  345. * Return: 0 for Success, else errno.
  346. */
  347. static int pcc_mbox_probe(struct platform_device *pdev)
  348. {
  349. int ret = 0;
  350. pcc_mbox_ctrl.chans = pcc_mbox_channels;
  351. pcc_mbox_ctrl.ops = &pcc_chan_ops;
  352. pcc_mbox_ctrl.dev = &pdev->dev;
  353. pr_info("Registering PCC driver as Mailbox controller\n");
  354. ret = mbox_controller_register(&pcc_mbox_ctrl);
  355. if (ret) {
  356. pr_err("Err registering PCC as Mailbox controller: %d\n", ret);
  357. ret = -ENODEV;
  358. }
  359. return ret;
  360. }
  361. struct platform_driver pcc_mbox_driver = {
  362. .probe = pcc_mbox_probe,
  363. .driver = {
  364. .name = "PCCT",
  365. .owner = THIS_MODULE,
  366. },
  367. };
  368. static int __init pcc_init(void)
  369. {
  370. int ret;
  371. struct platform_device *pcc_pdev;
  372. if (acpi_disabled)
  373. return -ENODEV;
  374. /* Check if PCC support is available. */
  375. ret = acpi_pcc_probe();
  376. if (ret) {
  377. pr_debug("ACPI PCC probe failed.\n");
  378. return -ENODEV;
  379. }
  380. pcc_pdev = platform_create_bundle(&pcc_mbox_driver,
  381. pcc_mbox_probe, NULL, 0, NULL, 0);
  382. if (IS_ERR(pcc_pdev)) {
  383. pr_debug("Err creating PCC platform bundle\n");
  384. return PTR_ERR(pcc_pdev);
  385. }
  386. return 0;
  387. }
  388. /*
  389. * Make PCC init postcore so that users of this mailbox
  390. * such as the ACPI Processor driver have it available
  391. * at their init.
  392. */
  393. postcore_initcall(pcc_init);