cppc_acpi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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. continue;
  268. pdomain = &(cpc_ptr->domain_info);
  269. cpumask_set_cpu(i, pr->shared_cpu_map);
  270. cpumask_set_cpu(i, covered_cpus);
  271. if (pdomain->num_processors <= 1)
  272. continue;
  273. /* Validate the Domain info */
  274. count_target = pdomain->num_processors;
  275. if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
  276. pr->shared_type = CPUFREQ_SHARED_TYPE_ALL;
  277. else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
  278. pr->shared_type = CPUFREQ_SHARED_TYPE_HW;
  279. else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
  280. pr->shared_type = CPUFREQ_SHARED_TYPE_ANY;
  281. for_each_possible_cpu(j) {
  282. if (i == j)
  283. continue;
  284. match_cpc_ptr = per_cpu(cpc_desc_ptr, j);
  285. if (!match_cpc_ptr)
  286. continue;
  287. match_pdomain = &(match_cpc_ptr->domain_info);
  288. if (match_pdomain->domain != pdomain->domain)
  289. continue;
  290. /* Here i and j are in the same domain */
  291. if (match_pdomain->num_processors != count_target) {
  292. retval = -EFAULT;
  293. goto err_ret;
  294. }
  295. if (pdomain->coord_type != match_pdomain->coord_type) {
  296. retval = -EFAULT;
  297. goto err_ret;
  298. }
  299. cpumask_set_cpu(j, covered_cpus);
  300. cpumask_set_cpu(j, pr->shared_cpu_map);
  301. }
  302. for_each_possible_cpu(j) {
  303. if (i == j)
  304. continue;
  305. match_pr = all_cpu_data[j];
  306. if (!match_pr)
  307. continue;
  308. match_cpc_ptr = per_cpu(cpc_desc_ptr, j);
  309. if (!match_cpc_ptr)
  310. continue;
  311. match_pdomain = &(match_cpc_ptr->domain_info);
  312. if (match_pdomain->domain != pdomain->domain)
  313. continue;
  314. match_pr->shared_type = pr->shared_type;
  315. cpumask_copy(match_pr->shared_cpu_map,
  316. pr->shared_cpu_map);
  317. }
  318. }
  319. err_ret:
  320. for_each_possible_cpu(i) {
  321. pr = all_cpu_data[i];
  322. if (!pr)
  323. continue;
  324. /* Assume no coordination on any error parsing domain info */
  325. if (retval) {
  326. cpumask_clear(pr->shared_cpu_map);
  327. cpumask_set_cpu(i, pr->shared_cpu_map);
  328. pr->shared_type = CPUFREQ_SHARED_TYPE_ALL;
  329. }
  330. }
  331. free_cpumask_var(covered_cpus);
  332. return retval;
  333. }
  334. EXPORT_SYMBOL_GPL(acpi_get_psd_map);
  335. static int register_pcc_channel(int pcc_subspace_idx)
  336. {
  337. struct acpi_pcct_hw_reduced *cppc_ss;
  338. unsigned int len;
  339. u64 usecs_lat;
  340. if (pcc_subspace_idx >= 0) {
  341. pcc_channel = pcc_mbox_request_channel(&cppc_mbox_cl,
  342. pcc_subspace_idx);
  343. if (IS_ERR(pcc_channel)) {
  344. pr_err("Failed to find PCC communication channel\n");
  345. return -ENODEV;
  346. }
  347. /*
  348. * The PCC mailbox controller driver should
  349. * have parsed the PCCT (global table of all
  350. * PCC channels) and stored pointers to the
  351. * subspace communication region in con_priv.
  352. */
  353. cppc_ss = pcc_channel->con_priv;
  354. if (!cppc_ss) {
  355. pr_err("No PCC subspace found for CPPC\n");
  356. return -ENODEV;
  357. }
  358. /*
  359. * This is the shared communication region
  360. * for the OS and Platform to communicate over.
  361. */
  362. comm_base_addr = cppc_ss->base_address;
  363. len = cppc_ss->length;
  364. /*
  365. * cppc_ss->latency is just a Nominal value. In reality
  366. * the remote processor could be much slower to reply.
  367. * So add an arbitrary amount of wait on top of Nominal.
  368. */
  369. usecs_lat = NUM_RETRIES * cppc_ss->latency;
  370. deadline = ns_to_ktime(usecs_lat * NSEC_PER_USEC);
  371. pcc_mrtt = cppc_ss->min_turnaround_time;
  372. pcc_mpar = cppc_ss->max_access_rate;
  373. pcc_comm_addr = acpi_os_ioremap(comm_base_addr, len);
  374. if (!pcc_comm_addr) {
  375. pr_err("Failed to ioremap PCC comm region mem\n");
  376. return -ENOMEM;
  377. }
  378. /* Set flag so that we dont come here for each CPU. */
  379. pcc_channel_acquired = true;
  380. }
  381. return 0;
  382. }
  383. /*
  384. * An example CPC table looks like the following.
  385. *
  386. * Name(_CPC, Package()
  387. * {
  388. * 17,
  389. * NumEntries
  390. * 1,
  391. * // Revision
  392. * ResourceTemplate(){Register(PCC, 32, 0, 0x120, 2)},
  393. * // Highest Performance
  394. * ResourceTemplate(){Register(PCC, 32, 0, 0x124, 2)},
  395. * // Nominal Performance
  396. * ResourceTemplate(){Register(PCC, 32, 0, 0x128, 2)},
  397. * // Lowest Nonlinear Performance
  398. * ResourceTemplate(){Register(PCC, 32, 0, 0x12C, 2)},
  399. * // Lowest Performance
  400. * ResourceTemplate(){Register(PCC, 32, 0, 0x130, 2)},
  401. * // Guaranteed Performance Register
  402. * ResourceTemplate(){Register(PCC, 32, 0, 0x110, 2)},
  403. * // Desired Performance Register
  404. * ResourceTemplate(){Register(SystemMemory, 0, 0, 0, 0)},
  405. * ..
  406. * ..
  407. * ..
  408. *
  409. * }
  410. * Each Register() encodes how to access that specific register.
  411. * e.g. a sample PCC entry has the following encoding:
  412. *
  413. * Register (
  414. * PCC,
  415. * AddressSpaceKeyword
  416. * 8,
  417. * //RegisterBitWidth
  418. * 8,
  419. * //RegisterBitOffset
  420. * 0x30,
  421. * //RegisterAddress
  422. * 9
  423. * //AccessSize (subspace ID)
  424. * 0
  425. * )
  426. * }
  427. */
  428. /**
  429. * acpi_cppc_processor_probe - Search for per CPU _CPC objects.
  430. * @pr: Ptr to acpi_processor containing this CPUs logical Id.
  431. *
  432. * Return: 0 for success or negative value for err.
  433. */
  434. int acpi_cppc_processor_probe(struct acpi_processor *pr)
  435. {
  436. struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
  437. union acpi_object *out_obj, *cpc_obj;
  438. struct cpc_desc *cpc_ptr;
  439. struct cpc_reg *gas_t;
  440. acpi_handle handle = pr->handle;
  441. unsigned int num_ent, i, cpc_rev;
  442. acpi_status status;
  443. int ret = -EFAULT;
  444. /* Parse the ACPI _CPC table for this cpu. */
  445. status = acpi_evaluate_object_typed(handle, "_CPC", NULL, &output,
  446. ACPI_TYPE_PACKAGE);
  447. if (ACPI_FAILURE(status)) {
  448. ret = -ENODEV;
  449. goto out_buf_free;
  450. }
  451. out_obj = (union acpi_object *) output.pointer;
  452. cpc_ptr = kzalloc(sizeof(struct cpc_desc), GFP_KERNEL);
  453. if (!cpc_ptr) {
  454. ret = -ENOMEM;
  455. goto out_buf_free;
  456. }
  457. /* First entry is NumEntries. */
  458. cpc_obj = &out_obj->package.elements[0];
  459. if (cpc_obj->type == ACPI_TYPE_INTEGER) {
  460. num_ent = cpc_obj->integer.value;
  461. } else {
  462. pr_debug("Unexpected entry type(%d) for NumEntries\n",
  463. cpc_obj->type);
  464. goto out_free;
  465. }
  466. /* Only support CPPCv2. Bail otherwise. */
  467. if (num_ent != CPPC_NUM_ENT) {
  468. pr_debug("Firmware exports %d entries. Expected: %d\n",
  469. num_ent, CPPC_NUM_ENT);
  470. goto out_free;
  471. }
  472. /* Second entry should be revision. */
  473. cpc_obj = &out_obj->package.elements[1];
  474. if (cpc_obj->type == ACPI_TYPE_INTEGER) {
  475. cpc_rev = cpc_obj->integer.value;
  476. } else {
  477. pr_debug("Unexpected entry type(%d) for Revision\n",
  478. cpc_obj->type);
  479. goto out_free;
  480. }
  481. if (cpc_rev != CPPC_REV) {
  482. pr_debug("Firmware exports revision:%d. Expected:%d\n",
  483. cpc_rev, CPPC_REV);
  484. goto out_free;
  485. }
  486. /* Iterate through remaining entries in _CPC */
  487. for (i = 2; i < num_ent; i++) {
  488. cpc_obj = &out_obj->package.elements[i];
  489. if (cpc_obj->type == ACPI_TYPE_INTEGER) {
  490. cpc_ptr->cpc_regs[i-2].type = ACPI_TYPE_INTEGER;
  491. cpc_ptr->cpc_regs[i-2].cpc_entry.int_value = cpc_obj->integer.value;
  492. } else if (cpc_obj->type == ACPI_TYPE_BUFFER) {
  493. gas_t = (struct cpc_reg *)
  494. cpc_obj->buffer.pointer;
  495. /*
  496. * The PCC Subspace index is encoded inside
  497. * the CPC table entries. The same PCC index
  498. * will be used for all the PCC entries,
  499. * so extract it only once.
  500. */
  501. if (gas_t->space_id == ACPI_ADR_SPACE_PLATFORM_COMM) {
  502. if (pcc_subspace_idx < 0)
  503. pcc_subspace_idx = gas_t->access_width;
  504. else if (pcc_subspace_idx != gas_t->access_width) {
  505. pr_debug("Mismatched PCC ids.\n");
  506. goto out_free;
  507. }
  508. } else if (gas_t->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY) {
  509. /* Support only PCC and SYS MEM type regs */
  510. pr_debug("Unsupported register type: %d\n", gas_t->space_id);
  511. goto out_free;
  512. }
  513. cpc_ptr->cpc_regs[i-2].type = ACPI_TYPE_BUFFER;
  514. memcpy(&cpc_ptr->cpc_regs[i-2].cpc_entry.reg, gas_t, sizeof(*gas_t));
  515. } else {
  516. pr_debug("Err in entry:%d in CPC table of CPU:%d \n", i, pr->id);
  517. goto out_free;
  518. }
  519. }
  520. /* Store CPU Logical ID */
  521. cpc_ptr->cpu_id = pr->id;
  522. /* Plug it into this CPUs CPC descriptor. */
  523. per_cpu(cpc_desc_ptr, pr->id) = cpc_ptr;
  524. /* Parse PSD data for this CPU */
  525. ret = acpi_get_psd(cpc_ptr, handle);
  526. if (ret)
  527. goto out_free;
  528. /* Register PCC channel once for all CPUs. */
  529. if (!pcc_channel_acquired) {
  530. ret = register_pcc_channel(pcc_subspace_idx);
  531. if (ret)
  532. goto out_free;
  533. }
  534. /* Everything looks okay */
  535. pr_debug("Parsed CPC struct for CPU: %d\n", pr->id);
  536. kfree(output.pointer);
  537. return 0;
  538. out_free:
  539. kfree(cpc_ptr);
  540. out_buf_free:
  541. kfree(output.pointer);
  542. return ret;
  543. }
  544. EXPORT_SYMBOL_GPL(acpi_cppc_processor_probe);
  545. /**
  546. * acpi_cppc_processor_exit - Cleanup CPC structs.
  547. * @pr: Ptr to acpi_processor containing this CPUs logical Id.
  548. *
  549. * Return: Void
  550. */
  551. void acpi_cppc_processor_exit(struct acpi_processor *pr)
  552. {
  553. struct cpc_desc *cpc_ptr;
  554. cpc_ptr = per_cpu(cpc_desc_ptr, pr->id);
  555. kfree(cpc_ptr);
  556. }
  557. EXPORT_SYMBOL_GPL(acpi_cppc_processor_exit);
  558. /*
  559. * Since cpc_read and cpc_write are called while holding pcc_lock, it should be
  560. * as fast as possible. We have already mapped the PCC subspace during init, so
  561. * we can directly write to it.
  562. */
  563. static int cpc_read(struct cpc_reg *reg, u64 *val)
  564. {
  565. int ret_val = 0;
  566. *val = 0;
  567. if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM) {
  568. void __iomem *vaddr = GET_PCC_VADDR(reg->address);
  569. switch (reg->bit_width) {
  570. case 8:
  571. *val = readb_relaxed(vaddr);
  572. break;
  573. case 16:
  574. *val = readw_relaxed(vaddr);
  575. break;
  576. case 32:
  577. *val = readl_relaxed(vaddr);
  578. break;
  579. case 64:
  580. *val = readq_relaxed(vaddr);
  581. break;
  582. default:
  583. pr_debug("Error: Cannot read %u bit width from PCC\n",
  584. reg->bit_width);
  585. ret_val = -EFAULT;
  586. }
  587. } else
  588. ret_val = acpi_os_read_memory((acpi_physical_address)reg->address,
  589. val, reg->bit_width);
  590. return ret_val;
  591. }
  592. static int cpc_write(struct cpc_reg *reg, u64 val)
  593. {
  594. int ret_val = 0;
  595. if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM) {
  596. void __iomem *vaddr = GET_PCC_VADDR(reg->address);
  597. switch (reg->bit_width) {
  598. case 8:
  599. writeb_relaxed(val, vaddr);
  600. break;
  601. case 16:
  602. writew_relaxed(val, vaddr);
  603. break;
  604. case 32:
  605. writel_relaxed(val, vaddr);
  606. break;
  607. case 64:
  608. writeq_relaxed(val, vaddr);
  609. break;
  610. default:
  611. pr_debug("Error: Cannot write %u bit width to PCC\n",
  612. reg->bit_width);
  613. ret_val = -EFAULT;
  614. break;
  615. }
  616. } else
  617. ret_val = acpi_os_write_memory((acpi_physical_address)reg->address,
  618. val, reg->bit_width);
  619. return ret_val;
  620. }
  621. /**
  622. * cppc_get_perf_caps - Get a CPUs performance capabilities.
  623. * @cpunum: CPU from which to get capabilities info.
  624. * @perf_caps: ptr to cppc_perf_caps. See cppc_acpi.h
  625. *
  626. * Return: 0 for success with perf_caps populated else -ERRNO.
  627. */
  628. int cppc_get_perf_caps(int cpunum, struct cppc_perf_caps *perf_caps)
  629. {
  630. struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum);
  631. struct cpc_register_resource *highest_reg, *lowest_reg, *ref_perf,
  632. *nom_perf;
  633. u64 high, low, ref, nom;
  634. int ret = 0;
  635. if (!cpc_desc) {
  636. pr_debug("No CPC descriptor for CPU:%d\n", cpunum);
  637. return -ENODEV;
  638. }
  639. highest_reg = &cpc_desc->cpc_regs[HIGHEST_PERF];
  640. lowest_reg = &cpc_desc->cpc_regs[LOWEST_PERF];
  641. ref_perf = &cpc_desc->cpc_regs[REFERENCE_PERF];
  642. nom_perf = &cpc_desc->cpc_regs[NOMINAL_PERF];
  643. spin_lock(&pcc_lock);
  644. /* Are any of the regs PCC ?*/
  645. if ((highest_reg->cpc_entry.reg.space_id == ACPI_ADR_SPACE_PLATFORM_COMM) ||
  646. (lowest_reg->cpc_entry.reg.space_id == ACPI_ADR_SPACE_PLATFORM_COMM) ||
  647. (ref_perf->cpc_entry.reg.space_id == ACPI_ADR_SPACE_PLATFORM_COMM) ||
  648. (nom_perf->cpc_entry.reg.space_id == ACPI_ADR_SPACE_PLATFORM_COMM)) {
  649. /* Ring doorbell once to update PCC subspace */
  650. if (send_pcc_cmd(CMD_READ) < 0) {
  651. ret = -EIO;
  652. goto out_err;
  653. }
  654. }
  655. cpc_read(&highest_reg->cpc_entry.reg, &high);
  656. perf_caps->highest_perf = high;
  657. cpc_read(&lowest_reg->cpc_entry.reg, &low);
  658. perf_caps->lowest_perf = low;
  659. cpc_read(&ref_perf->cpc_entry.reg, &ref);
  660. perf_caps->reference_perf = ref;
  661. cpc_read(&nom_perf->cpc_entry.reg, &nom);
  662. perf_caps->nominal_perf = nom;
  663. if (!ref)
  664. perf_caps->reference_perf = perf_caps->nominal_perf;
  665. if (!high || !low || !nom)
  666. ret = -EFAULT;
  667. out_err:
  668. spin_unlock(&pcc_lock);
  669. return ret;
  670. }
  671. EXPORT_SYMBOL_GPL(cppc_get_perf_caps);
  672. /**
  673. * cppc_get_perf_ctrs - Read a CPUs performance feedback counters.
  674. * @cpunum: CPU from which to read counters.
  675. * @perf_fb_ctrs: ptr to cppc_perf_fb_ctrs. See cppc_acpi.h
  676. *
  677. * Return: 0 for success with perf_fb_ctrs populated else -ERRNO.
  678. */
  679. int cppc_get_perf_ctrs(int cpunum, struct cppc_perf_fb_ctrs *perf_fb_ctrs)
  680. {
  681. struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum);
  682. struct cpc_register_resource *delivered_reg, *reference_reg;
  683. u64 delivered, reference;
  684. int ret = 0;
  685. if (!cpc_desc) {
  686. pr_debug("No CPC descriptor for CPU:%d\n", cpunum);
  687. return -ENODEV;
  688. }
  689. delivered_reg = &cpc_desc->cpc_regs[DELIVERED_CTR];
  690. reference_reg = &cpc_desc->cpc_regs[REFERENCE_CTR];
  691. spin_lock(&pcc_lock);
  692. /* Are any of the regs PCC ?*/
  693. if ((delivered_reg->cpc_entry.reg.space_id == ACPI_ADR_SPACE_PLATFORM_COMM) ||
  694. (reference_reg->cpc_entry.reg.space_id == ACPI_ADR_SPACE_PLATFORM_COMM)) {
  695. /* Ring doorbell once to update PCC subspace */
  696. if (send_pcc_cmd(CMD_READ) < 0) {
  697. ret = -EIO;
  698. goto out_err;
  699. }
  700. }
  701. cpc_read(&delivered_reg->cpc_entry.reg, &delivered);
  702. cpc_read(&reference_reg->cpc_entry.reg, &reference);
  703. if (!delivered || !reference) {
  704. ret = -EFAULT;
  705. goto out_err;
  706. }
  707. perf_fb_ctrs->delivered = delivered;
  708. perf_fb_ctrs->reference = reference;
  709. perf_fb_ctrs->delivered -= perf_fb_ctrs->prev_delivered;
  710. perf_fb_ctrs->reference -= perf_fb_ctrs->prev_reference;
  711. perf_fb_ctrs->prev_delivered = delivered;
  712. perf_fb_ctrs->prev_reference = reference;
  713. out_err:
  714. spin_unlock(&pcc_lock);
  715. return ret;
  716. }
  717. EXPORT_SYMBOL_GPL(cppc_get_perf_ctrs);
  718. /**
  719. * cppc_set_perf - Set a CPUs performance controls.
  720. * @cpu: CPU for which to set performance controls.
  721. * @perf_ctrls: ptr to cppc_perf_ctrls. See cppc_acpi.h
  722. *
  723. * Return: 0 for success, -ERRNO otherwise.
  724. */
  725. int cppc_set_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls)
  726. {
  727. struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu);
  728. struct cpc_register_resource *desired_reg;
  729. int ret = 0;
  730. if (!cpc_desc) {
  731. pr_debug("No CPC descriptor for CPU:%d\n", cpu);
  732. return -ENODEV;
  733. }
  734. desired_reg = &cpc_desc->cpc_regs[DESIRED_PERF];
  735. spin_lock(&pcc_lock);
  736. /* If this is PCC reg, check if channel is free before writing */
  737. if (desired_reg->cpc_entry.reg.space_id == ACPI_ADR_SPACE_PLATFORM_COMM) {
  738. ret = check_pcc_chan();
  739. if (ret)
  740. goto busy_channel;
  741. }
  742. /*
  743. * Skip writing MIN/MAX until Linux knows how to come up with
  744. * useful values.
  745. */
  746. cpc_write(&desired_reg->cpc_entry.reg, perf_ctrls->desired_perf);
  747. /* Is this a PCC reg ?*/
  748. if (desired_reg->cpc_entry.reg.space_id == ACPI_ADR_SPACE_PLATFORM_COMM) {
  749. /* Ring doorbell so Remote can get our perf request. */
  750. if (send_pcc_cmd(CMD_WRITE) < 0)
  751. ret = -EIO;
  752. }
  753. busy_channel:
  754. spin_unlock(&pcc_lock);
  755. return ret;
  756. }
  757. EXPORT_SYMBOL_GPL(cppc_set_perf);