cppc_acpi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. /*
  2. * CPPC (Collaborative Processor Performance Control) methods used by CPUfreq drivers.
  3. *
  4. * (C) Copyright 2014, 2015 Linaro Ltd.
  5. * Author: Ashwin Chaugule <ashwin.chaugule@linaro.org>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; version 2
  10. * of the License.
  11. *
  12. * CPPC describes a few methods for controlling CPU performance using
  13. * information from a per CPU table called CPC. This table is described in
  14. * the ACPI v5.0+ specification. The table consists of a list of
  15. * registers which may be memory mapped or hardware registers and also may
  16. * include some static integer values.
  17. *
  18. * CPU performance is on an abstract continuous scale as against a discretized
  19. * P-state scale which is tied to CPU frequency only. In brief, the basic
  20. * operation involves:
  21. *
  22. * - OS makes a CPU performance request. (Can provide min and max bounds)
  23. *
  24. * - Platform (such as BMC) is free to optimize request within requested bounds
  25. * depending on power/thermal budgets etc.
  26. *
  27. * - Platform conveys its decision back to OS
  28. *
  29. * The communication between OS and platform occurs through another medium
  30. * called (PCC) Platform Communication Channel. This is a generic mailbox like
  31. * mechanism which includes doorbell semantics to indicate register updates.
  32. * See drivers/mailbox/pcc.c for details on PCC.
  33. *
  34. * Finer details about the PCC and CPPC spec are available in the ACPI v5.1 and
  35. * above specifications.
  36. */
  37. #define pr_fmt(fmt) "ACPI CPPC: " fmt
  38. #include <linux/cpufreq.h>
  39. #include <linux/delay.h>
  40. #include <linux/ktime.h>
  41. #include <acpi/cppc_acpi.h>
  42. /*
  43. * Lock to provide mutually exclusive access to the PCC
  44. * channel. e.g. When the remote updates the shared region
  45. * with new data, the reader needs to be protected from
  46. * other CPUs activity on the same channel.
  47. */
  48. static DEFINE_SPINLOCK(pcc_lock);
  49. /*
  50. * The cpc_desc structure contains the ACPI register details
  51. * as described in the per CPU _CPC tables. The details
  52. * include the type of register (e.g. PCC, System IO, FFH etc.)
  53. * and destination addresses which lets us READ/WRITE CPU performance
  54. * information using the appropriate I/O methods.
  55. */
  56. static DEFINE_PER_CPU(struct cpc_desc *, cpc_desc_ptr);
  57. /* This layer handles all the PCC specifics for CPPC. */
  58. static struct mbox_chan *pcc_channel;
  59. static void __iomem *pcc_comm_addr;
  60. static u64 comm_base_addr;
  61. static int pcc_subspace_idx = -1;
  62. static bool pcc_channel_acquired;
  63. static ktime_t deadline;
  64. static unsigned int pcc_mpar, pcc_mrtt;
  65. /* pcc mapped address + header size + offset within PCC subspace */
  66. #define GET_PCC_VADDR(offs) (pcc_comm_addr + 0x8 + (offs))
  67. /*
  68. * Arbitrary Retries in case the remote processor is slow to respond
  69. * to PCC commands. Keeping it high enough to cover emulators where
  70. * the processors run painfully slow.
  71. */
  72. #define NUM_RETRIES 500
  73. static int check_pcc_chan(void)
  74. {
  75. int ret = -EIO;
  76. struct acpi_pcct_shared_memory __iomem *generic_comm_base = pcc_comm_addr;
  77. ktime_t next_deadline = ktime_add(ktime_get(), deadline);
  78. /* Retry in case the remote processor was too slow to catch up. */
  79. while (!ktime_after(ktime_get(), next_deadline)) {
  80. /*
  81. * Per spec, prior to boot the PCC space wil be initialized by
  82. * platform and should have set the command completion bit when
  83. * PCC can be used by OSPM
  84. */
  85. if (readw_relaxed(&generic_comm_base->status) & PCC_CMD_COMPLETE) {
  86. ret = 0;
  87. break;
  88. }
  89. /*
  90. * Reducing the bus traffic in case this loop takes longer than
  91. * a few retries.
  92. */
  93. udelay(3);
  94. }
  95. return ret;
  96. }
  97. static int send_pcc_cmd(u16 cmd)
  98. {
  99. int ret = -EIO;
  100. struct acpi_pcct_shared_memory *generic_comm_base =
  101. (struct acpi_pcct_shared_memory *) pcc_comm_addr;
  102. static ktime_t last_cmd_cmpl_time, last_mpar_reset;
  103. static int mpar_count;
  104. unsigned int time_delta;
  105. /*
  106. * For CMD_WRITE we know for a fact the caller should have checked
  107. * the channel before writing to PCC space
  108. */
  109. if (cmd == CMD_READ) {
  110. ret = check_pcc_chan();
  111. if (ret)
  112. return ret;
  113. }
  114. /*
  115. * Handle the Minimum Request Turnaround Time(MRTT)
  116. * "The minimum amount of time that OSPM must wait after the completion
  117. * of a command before issuing the next command, in microseconds"
  118. */
  119. if (pcc_mrtt) {
  120. time_delta = ktime_us_delta(ktime_get(), last_cmd_cmpl_time);
  121. if (pcc_mrtt > time_delta)
  122. udelay(pcc_mrtt - time_delta);
  123. }
  124. /*
  125. * Handle the non-zero Maximum Periodic Access Rate(MPAR)
  126. * "The maximum number of periodic requests that the subspace channel can
  127. * support, reported in commands per minute. 0 indicates no limitation."
  128. *
  129. * This parameter should be ideally zero or large enough so that it can
  130. * handle maximum number of requests that all the cores in the system can
  131. * collectively generate. If it is not, we will follow the spec and just
  132. * not send the request to the platform after hitting the MPAR limit in
  133. * any 60s window
  134. */
  135. if (pcc_mpar) {
  136. if (mpar_count == 0) {
  137. time_delta = ktime_ms_delta(ktime_get(), last_mpar_reset);
  138. if (time_delta < 60 * MSEC_PER_SEC) {
  139. pr_debug("PCC cmd not sent due to MPAR limit");
  140. return -EIO;
  141. }
  142. last_mpar_reset = ktime_get();
  143. mpar_count = pcc_mpar;
  144. }
  145. mpar_count--;
  146. }
  147. /* Write to the shared comm region. */
  148. writew_relaxed(cmd, &generic_comm_base->command);
  149. /* Flip CMD COMPLETE bit */
  150. writew_relaxed(0, &generic_comm_base->status);
  151. /* Ring doorbell */
  152. ret = mbox_send_message(pcc_channel, &cmd);
  153. if (ret < 0) {
  154. pr_err("Err sending PCC mbox message. cmd:%d, ret:%d\n",
  155. cmd, ret);
  156. return ret;
  157. }
  158. /*
  159. * For READs we need to ensure the cmd completed to ensure
  160. * the ensuing read()s can proceed. For WRITEs we dont care
  161. * because the actual write()s are done before coming here
  162. * and the next READ or WRITE will check if the channel
  163. * is busy/free at the entry of this call.
  164. *
  165. * If Minimum Request Turnaround Time is non-zero, we need
  166. * to record the completion time of both READ and WRITE
  167. * command for proper handling of MRTT, so we need to check
  168. * for pcc_mrtt in addition to CMD_READ
  169. */
  170. if (cmd == CMD_READ || pcc_mrtt) {
  171. ret = check_pcc_chan();
  172. if (pcc_mrtt)
  173. last_cmd_cmpl_time = ktime_get();
  174. }
  175. mbox_client_txdone(pcc_channel, ret);
  176. return ret;
  177. }
  178. static void cppc_chan_tx_done(struct mbox_client *cl, void *msg, int ret)
  179. {
  180. if (ret < 0)
  181. pr_debug("TX did not complete: CMD sent:%x, ret:%d\n",
  182. *(u16 *)msg, ret);
  183. else
  184. pr_debug("TX completed. CMD sent:%x, ret:%d\n",
  185. *(u16 *)msg, ret);
  186. }
  187. struct mbox_client cppc_mbox_cl = {
  188. .tx_done = cppc_chan_tx_done,
  189. .knows_txdone = true,
  190. };
  191. static int acpi_get_psd(struct cpc_desc *cpc_ptr, acpi_handle handle)
  192. {
  193. int result = -EFAULT;
  194. acpi_status status = AE_OK;
  195. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  196. struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"};
  197. struct acpi_buffer state = {0, NULL};
  198. union acpi_object *psd = NULL;
  199. struct acpi_psd_package *pdomain;
  200. status = acpi_evaluate_object_typed(handle, "_PSD", NULL, &buffer,
  201. ACPI_TYPE_PACKAGE);
  202. if (ACPI_FAILURE(status))
  203. return -ENODEV;
  204. psd = buffer.pointer;
  205. if (!psd || psd->package.count != 1) {
  206. pr_debug("Invalid _PSD data\n");
  207. goto end;
  208. }
  209. pdomain = &(cpc_ptr->domain_info);
  210. state.length = sizeof(struct acpi_psd_package);
  211. state.pointer = pdomain;
  212. status = acpi_extract_package(&(psd->package.elements[0]),
  213. &format, &state);
  214. if (ACPI_FAILURE(status)) {
  215. pr_debug("Invalid _PSD data for CPU:%d\n", cpc_ptr->cpu_id);
  216. goto end;
  217. }
  218. if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) {
  219. pr_debug("Unknown _PSD:num_entries for CPU:%d\n", cpc_ptr->cpu_id);
  220. goto end;
  221. }
  222. if (pdomain->revision != ACPI_PSD_REV0_REVISION) {
  223. pr_debug("Unknown _PSD:revision for CPU: %d\n", cpc_ptr->cpu_id);
  224. goto end;
  225. }
  226. if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
  227. pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
  228. pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
  229. pr_debug("Invalid _PSD:coord_type for CPU:%d\n", cpc_ptr->cpu_id);
  230. goto end;
  231. }
  232. result = 0;
  233. end:
  234. kfree(buffer.pointer);
  235. return result;
  236. }
  237. /**
  238. * acpi_get_psd_map - Map the CPUs in a common freq domain.
  239. * @all_cpu_data: Ptrs to CPU specific CPPC data including PSD info.
  240. *
  241. * Return: 0 for success or negative value for err.
  242. */
  243. int acpi_get_psd_map(struct cpudata **all_cpu_data)
  244. {
  245. int count_target;
  246. int retval = 0;
  247. unsigned int i, j;
  248. cpumask_var_t covered_cpus;
  249. struct cpudata *pr, *match_pr;
  250. struct acpi_psd_package *pdomain;
  251. struct acpi_psd_package *match_pdomain;
  252. struct cpc_desc *cpc_ptr, *match_cpc_ptr;
  253. if (!zalloc_cpumask_var(&covered_cpus, GFP_KERNEL))
  254. return -ENOMEM;
  255. /*
  256. * Now that we have _PSD data from all CPUs, lets setup P-state
  257. * domain info.
  258. */
  259. for_each_possible_cpu(i) {
  260. pr = all_cpu_data[i];
  261. if (!pr)
  262. continue;
  263. if (cpumask_test_cpu(i, covered_cpus))
  264. continue;
  265. cpc_ptr = per_cpu(cpc_desc_ptr, i);
  266. if (!cpc_ptr) {
  267. retval = -EFAULT;
  268. goto err_ret;
  269. }
  270. pdomain = &(cpc_ptr->domain_info);
  271. cpumask_set_cpu(i, pr->shared_cpu_map);
  272. cpumask_set_cpu(i, covered_cpus);
  273. if (pdomain->num_processors <= 1)
  274. continue;
  275. /* Validate the Domain info */
  276. count_target = pdomain->num_processors;
  277. if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
  278. pr->shared_type = CPUFREQ_SHARED_TYPE_ALL;
  279. else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
  280. pr->shared_type = CPUFREQ_SHARED_TYPE_HW;
  281. else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
  282. pr->shared_type = CPUFREQ_SHARED_TYPE_ANY;
  283. for_each_possible_cpu(j) {
  284. if (i == j)
  285. continue;
  286. match_cpc_ptr = per_cpu(cpc_desc_ptr, j);
  287. if (!match_cpc_ptr) {
  288. retval = -EFAULT;
  289. goto err_ret;
  290. }
  291. match_pdomain = &(match_cpc_ptr->domain_info);
  292. if (match_pdomain->domain != pdomain->domain)
  293. continue;
  294. /* Here i and j are in the same domain */
  295. if (match_pdomain->num_processors != count_target) {
  296. retval = -EFAULT;
  297. goto err_ret;
  298. }
  299. if (pdomain->coord_type != match_pdomain->coord_type) {
  300. retval = -EFAULT;
  301. goto err_ret;
  302. }
  303. cpumask_set_cpu(j, covered_cpus);
  304. cpumask_set_cpu(j, pr->shared_cpu_map);
  305. }
  306. for_each_possible_cpu(j) {
  307. if (i == j)
  308. continue;
  309. match_pr = all_cpu_data[j];
  310. if (!match_pr)
  311. continue;
  312. match_cpc_ptr = per_cpu(cpc_desc_ptr, j);
  313. if (!match_cpc_ptr) {
  314. retval = -EFAULT;
  315. goto err_ret;
  316. }
  317. match_pdomain = &(match_cpc_ptr->domain_info);
  318. if (match_pdomain->domain != pdomain->domain)
  319. continue;
  320. match_pr->shared_type = pr->shared_type;
  321. cpumask_copy(match_pr->shared_cpu_map,
  322. pr->shared_cpu_map);
  323. }
  324. }
  325. err_ret:
  326. for_each_possible_cpu(i) {
  327. pr = all_cpu_data[i];
  328. if (!pr)
  329. continue;
  330. /* Assume no coordination on any error parsing domain info */
  331. if (retval) {
  332. cpumask_clear(pr->shared_cpu_map);
  333. cpumask_set_cpu(i, pr->shared_cpu_map);
  334. pr->shared_type = CPUFREQ_SHARED_TYPE_ALL;
  335. }
  336. }
  337. free_cpumask_var(covered_cpus);
  338. return retval;
  339. }
  340. EXPORT_SYMBOL_GPL(acpi_get_psd_map);
  341. static int register_pcc_channel(int pcc_subspace_idx)
  342. {
  343. struct acpi_pcct_hw_reduced *cppc_ss;
  344. unsigned int len;
  345. u64 usecs_lat;
  346. if (pcc_subspace_idx >= 0) {
  347. pcc_channel = pcc_mbox_request_channel(&cppc_mbox_cl,
  348. pcc_subspace_idx);
  349. if (IS_ERR(pcc_channel)) {
  350. pr_err("Failed to find PCC communication channel\n");
  351. return -ENODEV;
  352. }
  353. /*
  354. * The PCC mailbox controller driver should
  355. * have parsed the PCCT (global table of all
  356. * PCC channels) and stored pointers to the
  357. * subspace communication region in con_priv.
  358. */
  359. cppc_ss = pcc_channel->con_priv;
  360. if (!cppc_ss) {
  361. pr_err("No PCC subspace found for CPPC\n");
  362. return -ENODEV;
  363. }
  364. /*
  365. * This is the shared communication region
  366. * for the OS and Platform to communicate over.
  367. */
  368. comm_base_addr = cppc_ss->base_address;
  369. len = cppc_ss->length;
  370. /*
  371. * cppc_ss->latency is just a Nominal value. In reality
  372. * the remote processor could be much slower to reply.
  373. * So add an arbitrary amount of wait on top of Nominal.
  374. */
  375. usecs_lat = NUM_RETRIES * cppc_ss->latency;
  376. deadline = ns_to_ktime(usecs_lat * NSEC_PER_USEC);
  377. pcc_mrtt = cppc_ss->min_turnaround_time;
  378. pcc_mpar = cppc_ss->max_access_rate;
  379. pcc_comm_addr = acpi_os_ioremap(comm_base_addr, len);
  380. if (!pcc_comm_addr) {
  381. pr_err("Failed to ioremap PCC comm region mem\n");
  382. return -ENOMEM;
  383. }
  384. /* Set flag so that we dont come here for each CPU. */
  385. pcc_channel_acquired = true;
  386. }
  387. return 0;
  388. }
  389. /*
  390. * An example CPC table looks like the following.
  391. *
  392. * Name(_CPC, Package()
  393. * {
  394. * 17,
  395. * NumEntries
  396. * 1,
  397. * // Revision
  398. * ResourceTemplate(){Register(PCC, 32, 0, 0x120, 2)},
  399. * // Highest Performance
  400. * ResourceTemplate(){Register(PCC, 32, 0, 0x124, 2)},
  401. * // Nominal Performance
  402. * ResourceTemplate(){Register(PCC, 32, 0, 0x128, 2)},
  403. * // Lowest Nonlinear Performance
  404. * ResourceTemplate(){Register(PCC, 32, 0, 0x12C, 2)},
  405. * // Lowest Performance
  406. * ResourceTemplate(){Register(PCC, 32, 0, 0x130, 2)},
  407. * // Guaranteed Performance Register
  408. * ResourceTemplate(){Register(PCC, 32, 0, 0x110, 2)},
  409. * // Desired Performance Register
  410. * ResourceTemplate(){Register(SystemMemory, 0, 0, 0, 0)},
  411. * ..
  412. * ..
  413. * ..
  414. *
  415. * }
  416. * Each Register() encodes how to access that specific register.
  417. * e.g. a sample PCC entry has the following encoding:
  418. *
  419. * Register (
  420. * PCC,
  421. * AddressSpaceKeyword
  422. * 8,
  423. * //RegisterBitWidth
  424. * 8,
  425. * //RegisterBitOffset
  426. * 0x30,
  427. * //RegisterAddress
  428. * 9
  429. * //AccessSize (subspace ID)
  430. * 0
  431. * )
  432. * }
  433. */
  434. /**
  435. * acpi_cppc_processor_probe - Search for per CPU _CPC objects.
  436. * @pr: Ptr to acpi_processor containing this CPUs logical Id.
  437. *
  438. * Return: 0 for success or negative value for err.
  439. */
  440. int acpi_cppc_processor_probe(struct acpi_processor *pr)
  441. {
  442. struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
  443. union acpi_object *out_obj, *cpc_obj;
  444. struct cpc_desc *cpc_ptr;
  445. struct cpc_reg *gas_t;
  446. acpi_handle handle = pr->handle;
  447. unsigned int num_ent, i, cpc_rev;
  448. acpi_status status;
  449. int ret = -EFAULT;
  450. /* Parse the ACPI _CPC table for this cpu. */
  451. status = acpi_evaluate_object_typed(handle, "_CPC", NULL, &output,
  452. ACPI_TYPE_PACKAGE);
  453. if (ACPI_FAILURE(status)) {
  454. ret = -ENODEV;
  455. goto out_buf_free;
  456. }
  457. out_obj = (union acpi_object *) output.pointer;
  458. cpc_ptr = kzalloc(sizeof(struct cpc_desc), GFP_KERNEL);
  459. if (!cpc_ptr) {
  460. ret = -ENOMEM;
  461. goto out_buf_free;
  462. }
  463. /* First entry is NumEntries. */
  464. cpc_obj = &out_obj->package.elements[0];
  465. if (cpc_obj->type == ACPI_TYPE_INTEGER) {
  466. num_ent = cpc_obj->integer.value;
  467. } else {
  468. pr_debug("Unexpected entry type(%d) for NumEntries\n",
  469. cpc_obj->type);
  470. goto out_free;
  471. }
  472. /* Only support CPPCv2. Bail otherwise. */
  473. if (num_ent != CPPC_NUM_ENT) {
  474. pr_debug("Firmware exports %d entries. Expected: %d\n",
  475. num_ent, CPPC_NUM_ENT);
  476. goto out_free;
  477. }
  478. /* Second entry should be revision. */
  479. cpc_obj = &out_obj->package.elements[1];
  480. if (cpc_obj->type == ACPI_TYPE_INTEGER) {
  481. cpc_rev = cpc_obj->integer.value;
  482. } else {
  483. pr_debug("Unexpected entry type(%d) for Revision\n",
  484. cpc_obj->type);
  485. goto out_free;
  486. }
  487. if (cpc_rev != CPPC_REV) {
  488. pr_debug("Firmware exports revision:%d. Expected:%d\n",
  489. cpc_rev, CPPC_REV);
  490. goto out_free;
  491. }
  492. /* Iterate through remaining entries in _CPC */
  493. for (i = 2; i < num_ent; i++) {
  494. cpc_obj = &out_obj->package.elements[i];
  495. if (cpc_obj->type == ACPI_TYPE_INTEGER) {
  496. cpc_ptr->cpc_regs[i-2].type = ACPI_TYPE_INTEGER;
  497. cpc_ptr->cpc_regs[i-2].cpc_entry.int_value = cpc_obj->integer.value;
  498. } else if (cpc_obj->type == ACPI_TYPE_BUFFER) {
  499. gas_t = (struct cpc_reg *)
  500. cpc_obj->buffer.pointer;
  501. /*
  502. * The PCC Subspace index is encoded inside
  503. * the CPC table entries. The same PCC index
  504. * will be used for all the PCC entries,
  505. * so extract it only once.
  506. */
  507. if (gas_t->space_id == ACPI_ADR_SPACE_PLATFORM_COMM) {
  508. if (pcc_subspace_idx < 0)
  509. pcc_subspace_idx = gas_t->access_width;
  510. else if (pcc_subspace_idx != gas_t->access_width) {
  511. pr_debug("Mismatched PCC ids.\n");
  512. goto out_free;
  513. }
  514. } else if (gas_t->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY) {
  515. /* Support only PCC and SYS MEM type regs */
  516. pr_debug("Unsupported register type: %d\n", gas_t->space_id);
  517. goto out_free;
  518. }
  519. cpc_ptr->cpc_regs[i-2].type = ACPI_TYPE_BUFFER;
  520. memcpy(&cpc_ptr->cpc_regs[i-2].cpc_entry.reg, gas_t, sizeof(*gas_t));
  521. } else {
  522. pr_debug("Err in entry:%d in CPC table of CPU:%d \n", i, pr->id);
  523. goto out_free;
  524. }
  525. }
  526. /* Store CPU Logical ID */
  527. cpc_ptr->cpu_id = pr->id;
  528. /* Parse PSD data for this CPU */
  529. ret = acpi_get_psd(cpc_ptr, handle);
  530. if (ret)
  531. goto out_free;
  532. /* Register PCC channel once for all CPUs. */
  533. if (!pcc_channel_acquired) {
  534. ret = register_pcc_channel(pcc_subspace_idx);
  535. if (ret)
  536. goto out_free;
  537. }
  538. /* Plug PSD data into this CPUs CPC descriptor. */
  539. per_cpu(cpc_desc_ptr, pr->id) = cpc_ptr;
  540. /* Everything looks okay */
  541. pr_debug("Parsed CPC struct for CPU: %d\n", pr->id);
  542. kfree(output.pointer);
  543. return 0;
  544. out_free:
  545. kfree(cpc_ptr);
  546. out_buf_free:
  547. kfree(output.pointer);
  548. return ret;
  549. }
  550. EXPORT_SYMBOL_GPL(acpi_cppc_processor_probe);
  551. /**
  552. * acpi_cppc_processor_exit - Cleanup CPC structs.
  553. * @pr: Ptr to acpi_processor containing this CPUs logical Id.
  554. *
  555. * Return: Void
  556. */
  557. void acpi_cppc_processor_exit(struct acpi_processor *pr)
  558. {
  559. struct cpc_desc *cpc_ptr;
  560. cpc_ptr = per_cpu(cpc_desc_ptr, pr->id);
  561. kfree(cpc_ptr);
  562. }
  563. EXPORT_SYMBOL_GPL(acpi_cppc_processor_exit);
  564. /*
  565. * Since cpc_read and cpc_write are called while holding pcc_lock, it should be
  566. * as fast as possible. We have already mapped the PCC subspace during init, so
  567. * we can directly write to it.
  568. */
  569. static int cpc_read(struct cpc_reg *reg, u64 *val)
  570. {
  571. int ret_val = 0;
  572. *val = 0;
  573. if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM) {
  574. void __iomem *vaddr = GET_PCC_VADDR(reg->address);
  575. switch (reg->bit_width) {
  576. case 8:
  577. *val = readb_relaxed(vaddr);
  578. break;
  579. case 16:
  580. *val = readw_relaxed(vaddr);
  581. break;
  582. case 32:
  583. *val = readl_relaxed(vaddr);
  584. break;
  585. case 64:
  586. *val = readq_relaxed(vaddr);
  587. break;
  588. default:
  589. pr_debug("Error: Cannot read %u bit width from PCC\n",
  590. reg->bit_width);
  591. ret_val = -EFAULT;
  592. }
  593. } else
  594. ret_val = acpi_os_read_memory((acpi_physical_address)reg->address,
  595. val, reg->bit_width);
  596. return ret_val;
  597. }
  598. static int cpc_write(struct cpc_reg *reg, u64 val)
  599. {
  600. int ret_val = 0;
  601. if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM) {
  602. void __iomem *vaddr = GET_PCC_VADDR(reg->address);
  603. switch (reg->bit_width) {
  604. case 8:
  605. writeb_relaxed(val, vaddr);
  606. break;
  607. case 16:
  608. writew_relaxed(val, vaddr);
  609. break;
  610. case 32:
  611. writel_relaxed(val, vaddr);
  612. break;
  613. case 64:
  614. writeq_relaxed(val, vaddr);
  615. break;
  616. default:
  617. pr_debug("Error: Cannot write %u bit width to PCC\n",
  618. reg->bit_width);
  619. ret_val = -EFAULT;
  620. break;
  621. }
  622. } else
  623. ret_val = acpi_os_write_memory((acpi_physical_address)reg->address,
  624. val, reg->bit_width);
  625. return ret_val;
  626. }
  627. /**
  628. * cppc_get_perf_caps - Get a CPUs performance capabilities.
  629. * @cpunum: CPU from which to get capabilities info.
  630. * @perf_caps: ptr to cppc_perf_caps. See cppc_acpi.h
  631. *
  632. * Return: 0 for success with perf_caps populated else -ERRNO.
  633. */
  634. int cppc_get_perf_caps(int cpunum, struct cppc_perf_caps *perf_caps)
  635. {
  636. struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum);
  637. struct cpc_register_resource *highest_reg, *lowest_reg, *ref_perf,
  638. *nom_perf;
  639. u64 high, low, ref, nom;
  640. int ret = 0;
  641. if (!cpc_desc) {
  642. pr_debug("No CPC descriptor for CPU:%d\n", cpunum);
  643. return -ENODEV;
  644. }
  645. highest_reg = &cpc_desc->cpc_regs[HIGHEST_PERF];
  646. lowest_reg = &cpc_desc->cpc_regs[LOWEST_PERF];
  647. ref_perf = &cpc_desc->cpc_regs[REFERENCE_PERF];
  648. nom_perf = &cpc_desc->cpc_regs[NOMINAL_PERF];
  649. spin_lock(&pcc_lock);
  650. /* Are any of the regs PCC ?*/
  651. if ((highest_reg->cpc_entry.reg.space_id == ACPI_ADR_SPACE_PLATFORM_COMM) ||
  652. (lowest_reg->cpc_entry.reg.space_id == ACPI_ADR_SPACE_PLATFORM_COMM) ||
  653. (ref_perf->cpc_entry.reg.space_id == ACPI_ADR_SPACE_PLATFORM_COMM) ||
  654. (nom_perf->cpc_entry.reg.space_id == ACPI_ADR_SPACE_PLATFORM_COMM)) {
  655. /* Ring doorbell once to update PCC subspace */
  656. if (send_pcc_cmd(CMD_READ) < 0) {
  657. ret = -EIO;
  658. goto out_err;
  659. }
  660. }
  661. cpc_read(&highest_reg->cpc_entry.reg, &high);
  662. perf_caps->highest_perf = high;
  663. cpc_read(&lowest_reg->cpc_entry.reg, &low);
  664. perf_caps->lowest_perf = low;
  665. cpc_read(&ref_perf->cpc_entry.reg, &ref);
  666. perf_caps->reference_perf = ref;
  667. cpc_read(&nom_perf->cpc_entry.reg, &nom);
  668. perf_caps->nominal_perf = nom;
  669. if (!ref)
  670. perf_caps->reference_perf = perf_caps->nominal_perf;
  671. if (!high || !low || !nom)
  672. ret = -EFAULT;
  673. out_err:
  674. spin_unlock(&pcc_lock);
  675. return ret;
  676. }
  677. EXPORT_SYMBOL_GPL(cppc_get_perf_caps);
  678. /**
  679. * cppc_get_perf_ctrs - Read a CPUs performance feedback counters.
  680. * @cpunum: CPU from which to read counters.
  681. * @perf_fb_ctrs: ptr to cppc_perf_fb_ctrs. See cppc_acpi.h
  682. *
  683. * Return: 0 for success with perf_fb_ctrs populated else -ERRNO.
  684. */
  685. int cppc_get_perf_ctrs(int cpunum, struct cppc_perf_fb_ctrs *perf_fb_ctrs)
  686. {
  687. struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum);
  688. struct cpc_register_resource *delivered_reg, *reference_reg;
  689. u64 delivered, reference;
  690. int ret = 0;
  691. if (!cpc_desc) {
  692. pr_debug("No CPC descriptor for CPU:%d\n", cpunum);
  693. return -ENODEV;
  694. }
  695. delivered_reg = &cpc_desc->cpc_regs[DELIVERED_CTR];
  696. reference_reg = &cpc_desc->cpc_regs[REFERENCE_CTR];
  697. spin_lock(&pcc_lock);
  698. /* Are any of the regs PCC ?*/
  699. if ((delivered_reg->cpc_entry.reg.space_id == ACPI_ADR_SPACE_PLATFORM_COMM) ||
  700. (reference_reg->cpc_entry.reg.space_id == ACPI_ADR_SPACE_PLATFORM_COMM)) {
  701. /* Ring doorbell once to update PCC subspace */
  702. if (send_pcc_cmd(CMD_READ) < 0) {
  703. ret = -EIO;
  704. goto out_err;
  705. }
  706. }
  707. cpc_read(&delivered_reg->cpc_entry.reg, &delivered);
  708. cpc_read(&reference_reg->cpc_entry.reg, &reference);
  709. if (!delivered || !reference) {
  710. ret = -EFAULT;
  711. goto out_err;
  712. }
  713. perf_fb_ctrs->delivered = delivered;
  714. perf_fb_ctrs->reference = reference;
  715. perf_fb_ctrs->delivered -= perf_fb_ctrs->prev_delivered;
  716. perf_fb_ctrs->reference -= perf_fb_ctrs->prev_reference;
  717. perf_fb_ctrs->prev_delivered = delivered;
  718. perf_fb_ctrs->prev_reference = reference;
  719. out_err:
  720. spin_unlock(&pcc_lock);
  721. return ret;
  722. }
  723. EXPORT_SYMBOL_GPL(cppc_get_perf_ctrs);
  724. /**
  725. * cppc_set_perf - Set a CPUs performance controls.
  726. * @cpu: CPU for which to set performance controls.
  727. * @perf_ctrls: ptr to cppc_perf_ctrls. See cppc_acpi.h
  728. *
  729. * Return: 0 for success, -ERRNO otherwise.
  730. */
  731. int cppc_set_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls)
  732. {
  733. struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu);
  734. struct cpc_register_resource *desired_reg;
  735. int ret = 0;
  736. if (!cpc_desc) {
  737. pr_debug("No CPC descriptor for CPU:%d\n", cpu);
  738. return -ENODEV;
  739. }
  740. desired_reg = &cpc_desc->cpc_regs[DESIRED_PERF];
  741. spin_lock(&pcc_lock);
  742. /* If this is PCC reg, check if channel is free before writing */
  743. if (desired_reg->cpc_entry.reg.space_id == ACPI_ADR_SPACE_PLATFORM_COMM) {
  744. ret = check_pcc_chan();
  745. if (ret)
  746. goto busy_channel;
  747. }
  748. /*
  749. * Skip writing MIN/MAX until Linux knows how to come up with
  750. * useful values.
  751. */
  752. cpc_write(&desired_reg->cpc_entry.reg, perf_ctrls->desired_perf);
  753. /* Is this a PCC reg ?*/
  754. if (desired_reg->cpc_entry.reg.space_id == ACPI_ADR_SPACE_PLATFORM_COMM) {
  755. /* Ring doorbell so Remote can get our perf request. */
  756. if (send_pcc_cmd(CMD_WRITE) < 0)
  757. ret = -EIO;
  758. }
  759. busy_channel:
  760. spin_unlock(&pcc_lock);
  761. return ret;
  762. }
  763. EXPORT_SYMBOL_GPL(cppc_set_perf);